Home > database >  When running my code it always displays System.Data.SqlClient.SqlException: 'Incorrect syntax n
When running my code it always displays System.Data.SqlClient.SqlException: 'Incorrect syntax n

Time:05-02

I don't know what is wrong in my query and in my code when i run my code it always display System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'Table'.

 protected void btn_next_Click(object sender, EventArgs e)
    {

        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Table values("   Convert.ToInt32(lbl_id.Text)   ",'"   lbl_fullname.Text   "')", con);

        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Write("<script>alert('Error')</script>");
        }
        else
        {
            Response.Write("<script>alert('Error')</script>");
        }

CodePudding user response:

In case your model name is actually Table and you are using SQL Server, try to put this the way it is recomended => Insert into [dbo].[Table]. Remember Table is a reserved word in SQL.

CodePudding user response:

  1. First of all, I would like to tell you that this is not the correct or secure way to insert data into the database. Please do some research on it.

  2. If the table name is not "Table" in your database then please replace the actual table name with "Table" in the query.

  3. Once you open the connection, you must close it after the all execution is done.

Thanks

  • Related