vb.net - Error when inserting data into a MS Access table -
how can insert data ms access table? when try, getting error.
code:
if textbox1.text = nothing , textbox2.text = nothing msgbox("no username , password inserted") textbox1.focus() else if not con.state = connectionstate.open 'open connection if not yet open end if cmd.connection = con 'add data table cmd.commandtext = "insert logintable(username, password, typeofuser) values ('" & me.textbox1.text & "', '" & me.textbox2.text & "', '" & me.combobox1.text & "')" cmd.executenonquery() 'refresh data in list 'close connection con.close() end if
first, don't open connection:
con.open()
next, password
reserved word in ms access. need wrap password
in square brackets:
[password]
you concatenating strings instead of using paramaters:
cmd.parameters.add("@username", oledbtype.varchar).value = txtusername.text cmd.parameters.add("@password", oledbtype.varchar).value = txtpassword.text cmd.parameters.add("@typeofuser", oledbtype.varchar).value = cmbtypeofuser.text
look @ giving textbox
, combobox
controls proper name instead of using textbox1
, textbox2
, combobox1
. helps identify correctly each control:
txtusername txtpassword cmbtypeofuser
move away using msgbox
, use messagebox.show
. msgbox
exists vb6 , ends delegating messagebox
anyway makes sense use messagebox.show
:
messagebox.show("no username , password inserted")
lastly consider implementing using close , dispose of sql objects:
using cmd new oledbcommand(command, connection) end using
all code this:
if txtusername.text = nothing , txtpassword.text = nothing messagebox.show("no username , password inserted") textbox1.focus() else using con new oledbconnection(connectionstring), cmd new oledbcommand("insert [logintable] ([username], [password], [typeofuser]) values (@username, @password, @typeofuser)", con) con.open() cmd.parameters.add("@username", oledbtype.varchar).value = txtusername.text cmd.parameters.add("@password", oledbtype.varchar).value = txtpassword.text cmd.parameters.add("@typeofuser", oledbtype.varchar).value = cmbtypeofuser.text cmd.executenonquery() end using end if
it's outside scope of question @ encrypting passwords. storing them plain text bad practice. have @ question; best way store password in database, may give ideas on how best this.
Comments
Post a Comment