SQL syntax error when trying to insert new record into Access database -
i have simple asp.net form individual fill out, , code used on particular page works in several other locations, page giving me issue.
it's saying there syntax error in insert statement. can see wrong it?
the click event code:
cmdinsert.commandtext = "insert position (com_id, stu_id, pos_startdate, pos_type, pos_description, pos_title) values (' " & ddlcompany.selectedvalue & " ', ' " & ddlstudent.selectedvalue & " ', #" & calstartdate.selecteddate.date & "#, ' " & ddlpositiontype.selectedvalue & " ', ' " & txtdescription.text & " ', ' " & txtpositiontitle.text & " ');" 'msgbox(cmdinsert.commandtext) cmdinsert.commandtype = commandtype.text cmdinsert.connection = cnnoledb cmdinsert.executenonquery() txtpositiontitle.text = "" txtdescription.text = "record inserted." calstartdate.selecteddates.clear() cmdinsert.dispose()
the data captured form lines data type in database. ideas?
here's stack trace:
[oledbexception (0x80040e14): syntax error in insert statement.] system.data.oledb.oledbcommand.executecommandtexterrorhandling(oledbhresult hr) +1081356 system.data.oledb.oledbcommand.executecommandtextforsingleresult(tagdbparams dbparams, object& executeresult) +247 system.data.oledb.oledbcommand.executecommandtext(object& executeresult) +194 system.data.oledb.oledbcommand.executecommand(commandbehavior behavior, object& executeresult) +58 system.data.oledb.oledbcommand.executereaderinternal(commandbehavior behavior, string method) +167 system.data.oledb.oledbcommand.executenonquery() +113 tiger_recruiting.webform3.btnsavestudentrecord_click(object sender, eventargs e) in c:\users\garrett\downloads\tiger recruiting(3)\tiger recruiting(3)\tiger recruiting(3)\tiger recruiting\tiger recruiting\position.aspx.vb:28 system.web.ui.webcontrols.button.onclick(eventargs e) +141 system.web.ui.webcontrols.button.raisepostbackevent(string eventargument) +149 system.web.ui.webcontrols.button.system.web.ui.ipostbackeventhandler.raisepostbackevent(string eventargument) +39 system.web.ui.page.raisepostbackevent(ipostbackeventhandler sourcecontrol, string eventargument) +37 system.web.ui.page.raisepostbackevent(namevaluecollection postdata) +87 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +4225
the word position reserved keyword in ms-access-jet.
use table name need encapsulate square brackets
cmdinsert.commandtext = "insert [position] .......
a part this, code bad. not use string cancatenation build sql commands.
practice leads syntax error when in input there single quote or have other fields require particular formatting of input value. worst of problem of sql injection look here funny explanation
so code should written in way:
cmdinsert.commandtext = "insert [position] (com_id, stu_id, pos_startdate, pos_type, " + "pos_description, pos_title) values " + "(?,?,?,?,?,?)" cmdinsert.parameters.addwithvalue("@p1",ddlcompany.selectedvalue) cmdinsert.parameters.addwithvalue("@p2",ddlstudent.selectedvalue) cmdinsert.parameters.addwithvalue("@p3",calstartdate.selecteddate.date) cmdinsert.parameters.addwithvalue("@p4",ddlpositiontype.selectedvalue) cmdinsert.parameters.addwithvalue("@p5",txtdescription.text) cmdinsert.parameters.addwithvalue("@p6",txtpositiontitle.text) cmdinsert.connection = cnnoledb cmdinsert.executenonquery()
Comments
Post a Comment