Asda Asdga
Asda Asdga

Reputation: 97

ASP connection error : Incorrect syntax near the keyword 'User

I'm getting an error

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near the keyword 'User'.

My code:

SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\user\OneDrive\Documents\Visual Studio 2015\Projects\WebApplication2\WebApplication2\App_Data\Database1.mdf; Integrated Security = True");

protected void btnOK_Click(object sender, EventArgs e)
{
    con.Open();

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText =  "insert into User(name, status, type) values('"+txtName.Text+"', '"+txtStatus.Text+ "', '" + txtType.Text + "')";

    cmd.ExecuteNonQuery();
    con.Close();
    Response.Redirect("Webform1.aspx");
}

Upvotes: 1

Views: 453

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

User is reserved keyword in SQL, you should use square brackets.

Try this:

 cmd.CommandText =  "insert into [User](name, status, type) values('"+txtName.Text+"', '"+txtStatus.Text+ "', '" + txtType.Text + "')";

Upvotes: 3

Related Questions