c# - How to Dispose of Database and Connection properly -


in c# app, have list of catalogs want user able display , update on grid.

to make work display grid , set update command, msaccess database update whenever user changes in grid.

whenever "some thing catalog" button pressed, updategrid function run. function receives name of table , update query, list of parameters make update.

private void updategrid(string data, string queryupdate, list<tuple<string, oledbtype,int>> parameters, bool fuse, string description) {      tblname.text = description;      try     {         oledbconnection conn = (fuse ? new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=fusing.mdb") : new oledbconnection("provider=microsoft.jet.oledb.4.0; data source=materialdb.mdb"));          oledbdataadapter adapter = new oledbdataadapter("select * " + data, conn);          mydataset = new dataset();         adapter.fill(mydataset, data);         datatable mydatatable = mydataset.tables[data];          // create updatecommand.         oledbcommand command = new oledbcommand(queryupdate, conn);          // add parameters updatecommand.         foreach (var param in parameters)         {             command.parameters.add("@" + param.item1, param.item2, param.item3, param.item1);         }         command.parameters.add("@id", oledbtype.integer, 5, "id");         adapter.updatecommand = command;          // update db whenever row changes         mydatatable.rowchanged += (s, f) =>         {             adapter.update(mydatatable);         };         var = catalogsgrid.getbindingexpression(datagrid.itemssourceproperty);          catalogsgrid.datacontext = mydataset;         catalogsgrid.itemssource = mydataset.tables[0].defaultview;         binding nbe = new binding         {             source = mydataset,             mode = bindingmode.twoway,             updatesourcetrigger = updatesourcetrigger.propertychanged,             path = new propertypath(data),             bindsdirectlytosource = true         };         catalogsgrid.setbinding(datagrid.itemssourceproperty, nbe);          catalogsgrid.items.refresh();     }     catch (exception)     {         return;     } } 

this function works intended.

however, tried follow disposing guidelines, , put using in each database element. when this, , becomes:

using(oledbconnection conn = (fuse ? new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=fusing.mdb") : new oledbconnection("provider=microsoft.jet.oledb.4.0; data source=materialdb.mdb"))) { } 

then update stops working generic error tells update query wrong.

should put using around each connetion, dataadapter , command, or let them die whenever c# decides.

edit:

the code using next:

private void updategrid(string data, string queryupdate, list<tuple<string, oledbtype,int>> parameters, bool fuse, string description) {      tblname.text = description;      try      {         using (oledbconnection conn = (fuse ? new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=fusing.mdb") : new oledbconnection("provider=microsoft.jet.oledb.4.0; data source=materialdb.mdb")))         {             using (oledbdataadapter adapter = new oledbdataadapter("select * " + data, conn))             {                  mydataset = new dataset();                 adapter.fill(mydataset, data);                 datatable mydatatable = mydataset.tables[data];                  // create updatecommand.                 using (oledbcommand command = new oledbcommand(queryupdate, conn))                 {                      // add parameters updatecommand.                     foreach (var param in parameters)                     {                         command.parameters.add("@" + param.item1, param.item2, param.item3, param.item1);                     }                     command.parameters.add("@id", oledbtype.integer, 5, "id");                     adapter.updatecommand = command;                      // update db whenever row changes                     mydatatable.rowchanged += (s, f) =>                     {                         adapter.update(mydatatable);                     };                     var = catalogsgrid.getbindingexpression(datagrid.itemssourceproperty);                      catalogsgrid.datacontext = mydataset;                     catalogsgrid.itemssource = mydataset.tables[0].defaultview;                     binding nbe = new binding                     {                         source = mydataset,                         mode = bindingmode.twoway,                         updatesourcetrigger = updatesourcetrigger.propertychanged,                         path = new propertypath(data),                         bindsdirectlytosource = true                     };                     catalogsgrid.setbinding(datagrid.itemssourceproperty, nbe);                      catalogsgrid.items.refresh();                 }             }         }      }     catch (exception)     {         return;     } } 

when code run, following exception.

exception

which search on internet said generic error whatever error happened in database update process, , 99% of time not error in query (and know not case, because before put in place using statements, working properly).


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

python - Error: Unresolved reference 'selenium' What is the reason? -

asp.net ajax - Jquery scroll to element just goes to top of page -