Professionally Save Data Grid View Changes To Database Table


hi prof ;

thant's code below concern

import data grid view , after finish changes click "btnsave" apply changes database table.

error message : the dataadapter.selectcommand property needs initialized. 

on line    

da.update(ds.tables["list"]);

on click btnsave please help

static oledbconnection con = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=e:\fullprojects\almogez\almogez\database\inventory.accdb");          public oledbcommand cmd = new oledbcommand ();        public oledbdataadapter da = new oledbdataadapter ();        public dataset ds = new dataset();                  public void execquery(string query)          {              try              {                  if (con.state == connectionstate.closed)                  {                     con.open();                     oledbcommand cmd = new oledbcommand(query, con);                     oledbdataadapter da = new oledbdataadapter(cmd);                     da.fill(ds,"list");                                   }              }              catch (exception ex)              {                  messagebox.show(ex.tostring());              }                       }    public void loadgrid()          {              dgvtext.datasource = ds.tables["list"];              oledbcommandbuilder cb = new oledbcommandbuilder(da);              dgvtext.rows[0].selected = true;            da.updatecommand = new oledbcommandbuilder(da).getupdatecommand();      private void testt_load(object sender, eventargs e)          {                execquery("select* tbl_items_try");              loadgrid();                       }      private void btn_click(object sender, eventargs e)          {            da.update(ds.tables["list"]);                     (ds.tables["list"]).acceptchanges();              loadgrid();                                 }

you should set selectcommand of oledbdataadapter before call fill method: http://stackoverflow.com/questions/15803816/the-selectcommand-property-has-not-been-initialized-before-calling-fill-in-wi

you creating new instance of oledbdataadapter class in execquery method. use public field:

public void execquery(string query)         {             try             {                 if (con.state == connectionstate.closed)                 {                    con.open();                    cmd = new oledbcommand(query, con);                    da = new oledbdataadapter(cmd);             da.selectcommand=new oledbcommand("select * yourtable", con);                    da.fill(ds,"list");                                  }             }             catch (exception ex)             {                 messagebox.show(ex.tostring());             }                     }


please refer documentation more information: https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommandbuilder%28v=vs.110%29.aspx?f=255&mspperror=-2147217396

"the oledbdataadapter not automatically generate sql statements required reconcile changes made dataset associated data source. however, can create oledbcommandbuilder object automatically generate sql statements single-table updates if set selectcommand property of oledbdataadapter."


hope helps.


please remember close threads marking helpful posts answer , start new thread if have new question. please don't ask several questions in same thread.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'