C# OleDb Exception "No value given for one or more required parameters" while trying to delete from Access database -
i have table "semester, subject, offer, result" "semester" & "subject" primary key. when use query
"delete course_information semester = 1 , subject = 'cse-414' ;
its working in access database exception when tried use in c# code.
moreover works if use "delete course_information semester = 1 ;
i want use both "subject" & "semester" in condition (because there different subject in same semester)
see code,
connection_string = aconnection.return_connectionstring(connection_string); string sql_query = "delete course_information semester = " + this.textbox1.text + " , subject = " + this.textbox2.text + " ;"; oledbconnection connect = new oledbconnection(connection_string); oledbcommand command = new oledbcommand(sql_query, connect); try { connect.open(); oledbdatareader reader = command.executereader(); messagebox.show("delete successful!"); connect.close(); updatedatabase(); } catch (exception ex) { messagebox.show(ex.message); }
include quotes around value this.textbox2.text
in working sample query.
" , subject = '" + this.textbox2.text + "';";
imagine this.textbox2.text
contains text foo. without adding quotes in where
clause db engine see ... semester = 1 , subject = foo
can't find in data source named foo
, assumes must parameter. need quotes signal db engine it's string literal value, 'foo'.
actually if switch parameter query, can avoid type of problem because won't need bother quotes in delete
statement. , parameter query safeguard against sql injection. if malicious user can enter ' or 'a' = 'a in this.textbox2.text
, rows in table deleted.
Comments
Post a Comment