septaug
septaug

Reputation: 61

Error inserting data into SQL

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

Upvotes: 2

Views: 100

Answers (3)

ASH
ASH

Reputation: 20302

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.";
    }

Upvotes: 0

SashaStojanovic
SashaStojanovic

Reputation: 341

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.

Upvotes: 0

Emre BOSTAN
Emre BOSTAN

Reputation: 43

 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.

Upvotes: 2

Related Questions