Share via


Incorrect syntax near the keyword 'user'.

Question

Thursday, August 18, 2011 3:54 PM

OK im having this error with my code when trying to do a SQL COMMAND

Heres my error

Incorrect syntax near the keyword 'user'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.

Source Error:

Line 83:                                         SqlCommand emailCMD = new SqlCommand(userSQL, cn);
Line 84:                                         cn.Open();
Line 85: int emailQuery = (int)emailCMD.ExecuteNonQuery(); Line 86:                                         cn.Close();
Line 87: 

Heres my code

Incorrect syntax near the keyword 'user'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.

Source Error: 


Line 83:                                         SqlCommand emailCMD = new SqlCommand(userSQL, cn);
Line 84:                                         cn.Open();
Line 85:                                         int emailQuery = (int)emailCMD.ExecuteNonQuery();
Line 86:                                         cn.Close();
Line 87: 
 

I need an answer quick, hopefully tonight kuzz this is for a school project and this is the last piece of it.\

Thanks in advance.

All replies (4)

Thursday, August 18, 2011 10:42 PM âś…Answered

     String userSQL = "INSERT INTO [user] (username, email, password, dob) VALUES ....

 

Another issue with your query: you need to use paramerized query. Search to find why.


Thursday, August 18, 2011 3:58 PM

Where is your userSQL string code?

Try to put user in a bracket [user] in your query. The user is a reserved word in SQL Server.


Thursday, August 18, 2011 3:58 PM

I would guess this is an issue in your query itself.  It looks like it is trying to reference either a field or table (or other object) called "user".  To fix this, you can enclose the field or table with square brackets like this:  select [user] from SomeTable

 

Matt


Thursday, August 18, 2011 10:18 PM

SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["GamePusherConnectionString"].ToString());
                                        String userSQL = "INSERT INTO user (username, email, password, dob) VALUES ('" + regUser + "', '" + regEmail + "', '" + regPass + "', '" + regBirth + "')";

                                        SqlCommand emailCMD = new SqlCommand(userSQL, cn);
                                        cn.Open();
                                        int emailQuery = (int)emailCMD.ExecuteNonQuery();
                                        cn.Close();

                                        if (emailQuery <= 0)
                                        {
                                            Response.Write("An error has Occured");
                                        }
                                        else
                                        {
                                            Response.Redirect("default.aspx");
                                        }