sql server - How to Refresh. Datagridview after adding data.im using sql database -


below code project. hope can me. in advance. :)

this sqlcontrol code

imports system.data imports system.data.sqlclient  public class sqlcontrol      public sqlcon new sqlconnection {.connectionstring = "server=xxx\sqlexpress;database=sqltest;user=sa;pwd=xxxx;"}     public sqlcmd sqlcommand 'allow fire query @ data base     public sqlda sqldataadapter     public sqldataset dataset     public dtable new datatable     public bs new bindingsource     'query parameters     public params new list(of sqlparameter)      public recordcount integer     public exception string      public function hasconnection() boolean         try             sqlcon.open()             sqlcon.close()             return true         catch ex exception             msgbox(ex.message)             return false         end try     end function      public sub execquery(byval query string)         try             sqlcon.open()              'create sql command             sqlcmd = new sqlcommand(query, sqlcon)              'load parameter sql command             params.foreach(sub(x) sqlcmd.parameters.add(x))              'clear parameter list             params.clear()              'excute command fill dataset             sqldataset = new dataset             'excute command wihtout adapter              sqlda = new sqldataadapter(sqlcmd)             recordcount = sqlda.fill(sqldataset)              sqlcon.close()         catch ex exception             exception = ex.message         end try          if sqlcon.state = connectionstate.open sqlcon.close()      end sub      public sub runquery(byval query string)         try             sqlcon.open()              sqlcmd = new sqlcommand(query, sqlcon)              'load sql records datagrid             sqlda = new sqldataadapter(sqlcmd)             sqldataset = new dataset             sqlda.fill(sqldataset)              sqlcon.close()         catch ex exception             msgbox(ex.message)              if sqlcon.state = connectionstate.open                 sqlcon.close()             end if         end try     end sub      public sub addmember(byval pc string, byval ip string, byval name string, byval email string, byval department string, byval location string,                      byval model string, byval specs string, byval dt string, byval asset string, byval rent string)          try             dim strinsert string = "insert members (pc,ip,name,email,department,location,model,specs,date,asset,rent) " & _                                  "values (" & _                                  "'" & pc & "'," & _                                  "'" & ip & "'," & _                                  "'" & name & "'," & _                                  "'" & email & "'," & _                                  "'" & department & "'," & _                                  "'" & location & "'," & _                                  "'" & model & "'," & _                                  "'" & specs & "'," & _                                  "'" & dt & "'," & _                                  "'" & asset & "'," & _                                  "'" & rent & "')"             msgbox(strinsert)              sqlcon.open()             sqlcmd = new sqlcommand(strinsert, sqlcon)              sqlcmd.executenonquery()             sqlcon.close()         catch ex exception             msgbox(ex.message)         end try     end sub end class 

and form code:

public class form1     private sql new sqlcontrol      private sub form1_load(byval sender system.object, byval e system.eventargs) handles mybase.load         'execute query , populate grid         sql.execquery("select * members")         loadgrid()          'disable save button         cmdsave.enabled = false     end sub      private sub loadgrid()         'if our data returned , populate grid & build update command         if sql.recordcount > 0             dgvdata.datasource = sql.sqldataset.tables(0)             dgvdata.rows(0).selected = true             sql.sqlda.updatecommand = new sqlclient.sqlcommandbuilder(sql.sqlda).getupdatecommand         end if     end sub      private sub dgvdata_cellvaluechanged(byval sender object, byval e system.windows.forms.datagridviewcelleventargs) handles dgvdata.cellvaluechanged         cmdsave.enabled = true      end sub      private sub dgvdata_rowsremoved(byval sender object, byval e system.windows.forms.datagridviewrowsremovedeventargs) handles dgvdata.rowsremoved         cmdsave.enabled = true     end sub      private sub cmdsave_click(byval sender system.object, byval e system.eventargs) handles cmdsave.click         'save update data base         try             sql.sqlda.update(sql.sqldataset) ' do: data validation         catch ex exception             msgbox("already exists")         end try          'refresh grid data         loadgrid()          'disable save button         cmdsave.enabled = false      end sub      private sub cmdadd_click(byval sender system.object, byval e system.eventargs) handles cmdadd.click          if trim(txtpc.text) = ""             msgbox("please fill out pc name.")             exit sub         end if          if trim(txtip.text) = ""             msgbox("please fill out ip address.")             exit sub         end if          'query user         sql.runquery("select * members members.pc = '" & txtpc.text & "'")          if sql.sqldataset.tables(0).rows.count > 0             msgbox("the name have enter enter exists")             exit sub         end if         sql.runquery("select * members members.ip = '" & txtip.text & "'")         if sql.sqldataset.tables(0).rows.count > 0             msgbox("the ip address have enter exists!")             exit sub         end if          sql.runquery("select * members members.asset = '" & txtasset.text & "'")         if sql.sqldataset.tables(0).rows.count > 0             msgbox("the asset have enter exists")             exit sub         else              createuser()             txtpc.clear()             txtip.clear()             txtname.clear()             txtemail.clear()             txtdepartment.clear()             txtlocation.clear()             txtmodel.clear()             txtspecs.clear()             txtdt.clear()             txtasset.clear()             txtrent.clear()         end if     end sub      public sub createuser()         sql.addmember(txtpc.text, txtip.text, txtname.text, txtemail.text, txtdepartment.text,                   txtlocation.text, txtmodel.text, txtspecs.text, txtdt.text, txtasset.text, txtrent.text)     end sub  end class 

i don't know how refresh datagridview

this works. copy , paste load form.

public sub refreshusergrid()     ' run query     sql.execquery("select * members")     if sql.sqldataset.tables.count > 0         dgvdata.datasource = sql.sqldataset.tables(0)         dgvdata.rows(0).selected = true     end if  end sub 

and copy , paste refreshusergrid() @ add command.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -