c# - Trouble with Update command for datagridview and npgsql -


i'm having issues npgsqlcommandbuilder , datagridview.

there no problem filling datagridview data can't updatecommand work.

my code looks this

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using npgsql;  namespace golfklubben {     public partial class laggtillresultat : form     {         private databaseconnection dataconnection = new databaseconnection();         private bindingsource bindingsource1 = new bindingsource();         private npgsqldataadapter daresult = new npgsqldataadapter();          public laggtillresultat()         {             initializecomponent();             filltavlingar();             datagridview1.datasource = bindingsource1;             fillresultat();             datagridview1.columns[0].headertext = "deltagare";             datagridview1.columns[1].headertext = "poäng";         }          /// <summary>         /// metod för att fylla en combobox med tävlingar från databasen med en dataadapter          /// method filling combobox data database         /// </summary>         private void filltavlingar()         {             datetime date = datetime.now;             string strdate = date.tostring("yyyy-mm-dd");             try             {                 datatable table = new datatable();                 npgsqlconnection conn = new npgsqlconnection(dataconnection.getconnection());                 npgsqldataadapter da = new npgsqldataadapter();                 npgsqlcommand command = new npgsqlcommand("select tavlingsid, titel tavlingar datum > @datum order titel asc;", conn);                 command.parameters.addwithvalue("@datum", strdate);                 da.selectcommand = command;                  da.fill(table);                 cbtavling.datasource = table.defaultview;                 cbtavling.displaymember = "titel";                 cbtavling.valuemember = "tavlingsid";             }             catch (exception err)             {                 messagebox.show("ett fel uppstod när tävlingarna skulle hämtas " + err.tostring(), "fel vid uppdatering av handicap", messageboxbuttons.ok, messageboxicon.error);             }          }          /// <summary>         /// metod för att fylla en datagridview med resultat från databasen         /// method filling datagridview data datbase         /// </summary>         private void fillresultat()         {             if (cbtavling.selectedvalue == null)             {                 messagebox.show("det finns ingen tävling idag!\ndialogrutan kommer att stängas.");                 this.close();             }              try             {                 npgsqlconnection conn = new npgsqlconnection(dataconnection.getconnection());                 npgsqlcommand command = new npgsqlcommand("select golfid, resultat anmalningar tavlingsid = @tavlingsid order golfid asc;", conn);                 command.parameters.addwithvalue("@tavlingsid", cbtavling.selectedvalue.tostring());                  daresult = new npgsqldataadapter(command);                  npgsqlcommandbuilder cb = new npgsqlcommandbuilder(daresult);                  datatable dt = new datatable();                 dt.locale = system.globalization.cultureinfo.invariantculture;                  daresult.fill(dt);                 bindingsource1.datasource = dt;              }             catch (exception err)             {                 messagebox.show("fel" + err.tostring());             }          }          private void btnspara_click(object sender, eventargs e)         {              try             {                 daresult.update((datatable)bindingsource1.datasource);             }             catch (exception err)             {                 messagebox.show(err.tostring());             }              fillresultat();         }          private void cbtavling_selectedindexchanged(object sender, eventargs e)         {             fillresultat();         }          /// <summary>         /// hanterar formclosing eventet som körs när formuläret håller på att stängas, gömmer formuläret istället för att stänga det.         /// det här är för att det annars inte går att öppna upp formuläret igen.         /// </summary>         /// <param name="e"></param>         protected override void onformclosing(formclosingeventargs e)         {             this.hide();             e.cancel = true;         }          private void btnavbryt_click(object sender, eventargs e)         {             dialogresult result = messagebox.show("vill du stänga formuläret?", "stänger formuläret", messageboxbuttons.yesno, messageboxicon.warning);             if (result == dialogresult.yes)             {                 this.hide();             }             else                 return;         }     }   } 

i have used tutorial msdn try solve this. msdn

kindly use conn.open(); before executing commands

for example

           datatable table = new datatable();             npgsqlconnection conn = new npgsqlconnection(dataconnection.getconnection());             conn.open();             npgsqldataadapter da = new npgsqldataadapter();             npgsqlcommand command = new npgsqlcommand("select tavlingsid, titel tavlingar datum > @datum order titel asc;", conn);             command.parameters.addwithvalue("@datum", strdate);             da.selectcommand = command;              da.fill(table); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -