SAM J

SAM J

  • NA
  • 24
  • 33.2k

ERROR - Must Declare Scalar Variable - C#.net/SQL server 2005

Mar 10 2011 9:28 AM
I am writing the below code in C#.net for a windows application, and using SQL Server 2005.
private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int i = Convert.ToInt32(textBox2.Text);
                string str = textBox1.Text;
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = "Data Source=DUGDOO;Initial Catalog=master;User ID=sa;Password=sa";
                SqlCommand scd = new SqlCommand("INSERT INTO T1(name1, id) VALUES (@textBox1, @textBox2)", cnn);
                scd.Parameters.AddWithValue("@name1", SqlDbType.VarChar).Value = textBox1.Text;
                scd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);
                cnn.Open();
                scd.ExecuteNonQuery();
                MessageBox.Show("Inserted");
                cnn.Close();
            }
            catch (Exception e1)
            { MessageBox.Show(""+e1); }
        }

I execute the application, enter name(varchar), id(int) in the txtboxs i get error - "Must declare Scalar Variable at @textBox1" - Pls Help. I cudnt find any solution.
 

Answers (2)