excel - access database is not updating from vb.net -
excel - access database is not updating from vb.net -
i have access database named login.mdb,which has many tables.i want update table named "try".in tat table have 2 fields viz name , rollnumber.i want update rollnum of corresponding name.my code is:
public class form11 inherits system.windows.forms.form dim myconnection1 system.data.oledb.oledbconnection dim mycommand1 new system.data.oledb.oledbcommand dim sql string ''# .... private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click seek myconnection1.connectionstring = "provider=microsoft.jet.oledb.4.0;data source = c:\documents , settings\1001\desktop\subhedar sir\windowsapplication1\bin\login.mdb" myconnection1.open() mycommand1.connection = myconnection1 mycommand1.commandtext = "update seek set rollnumber = '" & textbox1.text & "' nam = '" & textbox2.text & "';" mycommand1.commandtype = commandtype.text mycommand1.connection = myconnection1 mycommand1.executenonquery() msgbox("done") myconnection1.close() grab ex exception msgbox(ex.tostring) end seek end sub end class
please tell me mam going wrong.i getting error: system.nullreferenceexception:object refernce not set instance of object.
several issues here:
"try" reserved word in databases. access fine, if ever moves cause problems. best prepared early. no query parameters (vulnerable sql injection hacks). seek entering';drop table [try];--
in 1 of textboxes current code. a mutual db connection whole class can cause contention issues , bottleneck did not close connection in block, potentially leave connection open , may cause database unavailable absolute path db in connection string fail @ deployment never created instance of connection object (that's error exception) you code should more this:
private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click using cn new oledbconnection("provider=microsoft.jet.oledb.4.0;data source = login.mdb"), _ cmd new oledbcommand("update [try] set rollnumber= ? nam= ? ;", cn) ''# note: don't utilize addwithvalue(), don't know info types cmd.parameters.addwithvalue("?", textbox1.text) cmd.parameters.addwithvalue("?", textbox2.text) seek cn.open() cmd.executenonquery() msgbox("done") grab ex exception msgbox(ex.tostring) end seek end using end sub
this code accounts all of issues listed above, not 1 causing current exception.
vb.net excel
Comments
Post a Comment