Need help with a simple query

glendalondon

New Member
Hi, I am just migrating to ASP.NET<BR><BR>I'm connection to an Access database and simply trying to insert a user record. I can dump the sql to the screen, paste in Access and run it and it works fine; however, when I run the code, I get an error that says: Syntax error in INSERT INTO statement<BR><BR>Here is the code, thanks for any help you can give:<BR><BR>protected void btnSubmit_Click(object sender, EventArgs e)<BR> {<BR> OleDbConnection conn = new OleDbConnection((string) Session["dbConnectionString"]);<BR> conn.Open();<BR> <BR> string sql = "insert into users (user_name, last_name, first_name, email, password)" <BR> + " values ('" + txtUserName.Text + "', '" <BR> + txtLastName.Text + "', '" + txtFirstName.Text + "', '"<BR> + txtEmail.Text + "', '" + txtPassword.Text + "');";<BR> //Response.Write("<font color=white>" + sql + "</font><BR>"); <BR> <BR> <BR> OleDbCommand command = new OleDbCommand();<BR> command.Connection = conn;<BR> command.CommandText = sql;<BR> //Response.Write("<font color=white>" + command.CommandText + "</font><BR>");<BR> <BR> try { <BR> command.ExecuteNonQuery();<BR> }<BR> catch (OleDbException ex) {<BR> lblMessage.Text = ex.Message + "<BR><BR>" + sql;<BR> }<BR> <BR> <BR> <BR> //Response.Write(count + " records affected");<BR> <BR> }<BR><BR><BR>I hope that comes out right.<BR><BR>Once again, thanks,<BR>PuzzledI have a hunch that if you took out the semi-colon in your sql string (not the semi-colon that ends the statement):<BR><BR>+ txtEmail.Text + "', '" + txtPassword.Text + "');";<BR><BR>...it might work. I run queries against Oracle, and I know that the semi-colon is needed for queries run in the SQL+ Worksheet, but it is not valid when used through ADO or OleDb. I could be wrong.....?Thanks anyway.<BR><BR>I actually ended up putting that in there as a last-resort effort. :^(<BR><BR>JeffI had the same problem on saturady go to <BR>this url and this Is exactly what you are looking for<BR>http://www.aspmessageboard.com/forum/asp.asp?M=442564&F=20&P=1You probably got a single quote in one of the variables or something. It looks like you've response.written the sql. Share it with the class.
 
Back
Top