vb.net - Parameterized SQL update query doesn't work -
i stuck hours , don't know do.
code doesn't work in updating student records using parameterized sql query in vb.net.
dim result integer cmd = new oledbcommand("update students set student_name=@name, address=@address, contact_no=@contact id_number=@id_number;", conn) conn.open() cmd.connection = conn cmd.parameters.addwithvalue("@id_number", txt_idnumber.text) cmd.parameters.addwithvalue("@name", txt_name.text.tostring) cmd.parameters.addwithvalue("@address", txt_address.tostring) cmd.parameters.addwithvalue("@contact", txt_contactno.text.tostring) result = cmd.executenonquery() if result = 1 msgbox("changes updated successfully.", vbinformation, "message") else msgbox("update unsuccessful.", vbcritical, "message") end if conn.close()
oledb doesn't recognize parameters name. in oledb parameters should added parameters collection in same order in have listed them in commandtext.
this means code tries update record id_number equal content of txt_contactno
just move parameter @id_number after adding first three
' no need use tostring on property of type string' cmd.parameters.addwithvalue("@name", txt_name.text) cmd.parameters.addwithvalue("@address", txt_address.text) cmd.parameters.addwithvalue("@contact", txt_contactno.text) cmd.parameters.addwithvalue("@id_number", txt_idnumber.text)
as side note, please aware of the problems of addwithvalue. handy shortcut pay price it
Comments
Post a Comment