2

I have a C# Winform app witch uses the following SQL insert code but for some reason i get an error, the statement is:

 private void button1_Click(object sender, EventArgs e)
        {
            if (desemp.Text != "" && valu.Text != "" && fs.Text != "" && sel.Text != "" && desc.Text != "" && ench.Text != "" && comp.Text != "")
            {
                cmd = new SqlCommand("insert into dbo.vidros(desempenho,valu,fs,sel,desc,enchimento,compo) values (@desemp,@valu,@fs,@sel,@desc,@ench,@comp)", con);
                con.Open();
                cmd.Parameters.AddWithValue("@desemp", desemp.Text);
                cmd.Parameters.AddWithValue("@valu", valu.Text);
                cmd.Parameters.AddWithValue("@fs", fs.Text);
                cmd.Parameters.AddWithValue("@sel", sel.Text);
                cmd.Parameters.AddWithValue("@desc", desc.Text);
                cmd.Parameters.AddWithValue("@ench", ench.Text);
                cmd.Parameters.AddWithValue("@comp", comp.Text);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("sucess!");
            }
            else
            {
                MessageBox.Show("Error!");
            }
        }

The error i get is: Incorrect Syntax near the word 'desc'. I lost hours trying to figure this one out and cant find the fault.

Can anyone help?

Thanks in advance

6
  • 4
    desc is a reserved word, for ordering. Commented Feb 20, 2017 at 15:58
  • Is it because desc is a keyword? Commented Feb 20, 2017 at 15:58
  • 1
    desc is indeed a keyword. Use [desc] (with square brackets) if you really want to stick to it, although, I would not recommend it. Commented Feb 20, 2017 at 15:59
  • @JonSkeet yup that got rid of the error but for some reason the insert does not work, i get a sucess message but no data got into the DB Commented Feb 20, 2017 at 16:02
  • you may also need to add cmd.CommandType before cmd.ExecuteNonQuery(); Commented Feb 20, 2017 at 16:07

3 Answers 3

2
 SqlParameter[] Prms = new SqlParameter[] {
            new SqlParameter("desemp", desemp.Text),
            new SqlParameter("valu", valu.Text),
            new SqlParameter("fs", fs.Text),
            new SqlParameter("sel", sel.Text),
            new SqlParameter("desc", desc.Text),
            new SqlParameter("ench", ench.Text),
            new SqlParameter("comp", comp.Text),
        };


cmd.Parameters.AddRange(Prms);

Check the cell names and use them like this.

Sign up to request clarification or add additional context in comments.

1 Comment

This is just a different way to add parameters. This answer doesn't address the error.
0

If you copy and paste that SQL code into your management studio, you will see that "desc" is highlighted meaning that it is a reserved keyword, used for ordering queries.

Just change desc to [desc] and it should work.

Comments

0

A Button Click Event, using textBoxes, may look like this:

using System.Data.SqlClient;

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
    try
    {
        string query = "insert into UserDetail(Name,Address) 
        values('" + txtName.Text + "','" + txtAddress.Text + "');";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        con.Open();
        da.SelectCommand.ExecuteNonQuery();
        con.Close();
        lblmessage.Text = "Data saved successfully.";
    }
    catch
    {
        con.Close();
        lblmessage.Text = "Error while saving data.";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.