Cannot update Access database in C# -
i trying build update button able edit data textbox
. using access database , having problems , cannot update it.
bellow code:
private void cmdupdate_click(object sender, eventargs e) { try { con = new oledbconnection(cs.dbconn); con.open(); string queryupdate = @"update tblfixing set title=@title, codebefore=@codebefore, codafter=@codafter, exp=@exp, example=@example, notes=@notes fixid=@fixid"; cmd = new oledbcommand(queryupdate); cmd.connection = con; cmd.parameters.add(new oledbparameter("@fixid", oledbtype.wchar, 20, "fixid")); cmd.parameters.add(new oledbparameter("@title", oledbtype.wchar, 255, "title")); cmd.parameters.add(new oledbparameter("@codebefore", oledbtype.wchar, 0, "codebefore")); cmd.parameters.add(new oledbparameter("@codafter", oledbtype.wchar, 0, "codafter")); cmd.parameters.add(new oledbparameter("@exp", oledbtype.wchar, 0, "exp")); cmd.parameters.add(new oledbparameter("@example", oledbtype.wchar, 0, "example")); cmd.parameters.add(new oledbparameter("@notes", oledbtype.wchar, 255, "notes")); cmd.parameters["@fixid"].value = cmbid.text.trim(); cmd.parameters["@title"].value = txttitulo.text.trim(); cmd.parameters["@codbefore"].value = rchcodbefore.text.trim(); cmd.parameters["@codafter"].value = rchcodafter.text.trim(); cmd.parameters["@exp"].value = rchexplicacao.text.trim(); cmd.parameters["@example"].value = rchexemplo.text.trim(); cmd.parameters["@notes"].value = rchnotas.text.trim(); cmd.executenonquery(); messagebox.show("successful updated", "information", messageboxbuttons.ok, messageboxicon.information); con.close(); } catch (exception ex) { messagebox.show("error\ndetails: " + ex.message, "error", messageboxbuttons.ok, messageboxicon.error); } }
i debug code , data not update. should do?
oledb doesn't use named parameters. appear in index order.
you have @fixid
parameter first parameter added collection, when @ sql string, it's last parameter in string.
change parameter order:
cmd.parameters.add(new oledbparameter("@title", oledbtype.wchar, 255, "title")); cmd.parameters.add(new oledbparameter("@codebefore", oledbtype.wchar, 0, "codebefore")); cmd.parameters.add(new oledbparameter("@codafter", oledbtype.wchar, 0, "codafter")); cmd.parameters.add(new oledbparameter("@exp", oledbtype.wchar, 0, "exp")); cmd.parameters.add(new oledbparameter("@example", oledbtype.wchar, 0, "example")); cmd.parameters.add(new oledbparameter("@notes", oledbtype.wchar, 255, "notes")); cmd.parameters.add(new oledbparameter("@fixid", oledbtype.wchar, 20, "fixid"));
Comments
Post a Comment