none
Declaring Scalar variable RRS feed

  • Question

  • Hi

    I developed an application on Visual C#, connected to the sql management studio. I've been able to get data from the database successfully but have not been able to insert into the database. I'm having an sqlException {"Must declare the scalar variable \"@\"."} and here's my code please what I'm I missing?

            private void button1_Click(object sender, EventArgs e)
            {
                
                string connectionString = @"Data Source=JEREMIAH-HP\SQLEXPRESS;Initial Catalog=CourseInfo;Integrated Security=True";
                SqlConnection sqlCon = new SqlConnection(connectionString);
                SqlCommand command=new SqlCommand("INSERT INTO STUDINFO_TBL(MATRICNO,FIRST_NAME, LAST_NAME, DEPT,LEVEL,CGPA,MODEOFENTRY) VALUES (@MATRICNO, @ FIRST_NAME, @LAST_NAME, @DEPT, @LEVEL, @CGPA, @MODEOFENTRY),sqlCon)");

                command.Parameters.AddWithValue("@MATRICNO", matno.Text);
                command.Parameters.AddWithValue("@FIRST_NAME", frstname.Text);
                command.Parameters.AddWithValue("@LAST_NAME", lastname.Text);
                command.Parameters.AddWithValue("@DEPT", department.Text);
                command.Parameters.AddWithValue("@LEVEL", level.Text);
                command.Parameters.AddWithValue("@CGPA", mycgpa.Text);
                command.Parameters.AddWithValue("@MODEOFENTRY", modeofentry.Text);
                command.Connection = sqlCon;

               sqlCon.Open();
               command.ExecuteNonQuery();
               sqlCon.Close();

               MessageBox.Show("Record Updated!");
               Close();
            }

    • Moved by Bob Beauchemin Thursday, November 7, 2013 4:22 PM Moved to a more relevent forum
    Thursday, November 7, 2013 9:11 AM

Answers

  • I guess the issue is with the SqlCommand string; There is a space in FIRST_NAME parameter between '@' and the name. And also you specified the sqlCon object inside the command string. The actual SqlCommand will be as follows:

    SqlCommand command=new SqlCommand("INSERT INTO STUDINFO_TBL(MATRICNO,FIRST_NAME, LAST_NAME, DEPT,LEVEL,CGPA,MODEOFENTRY) VALUES (@MATRICNO, @FIRST_NAME, @LAST_NAME, @DEPT, @LEVEL, @CGPA, @MODEOFENTRY)", sqlCon);

    Try this.


    Krishnakumar S

    • Proposed as answer by Olaf HelperMVP Thursday, November 7, 2013 9:40 AM
    • Marked as answer by jerofad Thursday, November 7, 2013 10:09 AM
    Thursday, November 7, 2013 9:23 AM

All replies

  • I guess the issue is with the SqlCommand string; There is a space in FIRST_NAME parameter between '@' and the name. And also you specified the sqlCon object inside the command string. The actual SqlCommand will be as follows:

    SqlCommand command=new SqlCommand("INSERT INTO STUDINFO_TBL(MATRICNO,FIRST_NAME, LAST_NAME, DEPT,LEVEL,CGPA,MODEOFENTRY) VALUES (@MATRICNO, @FIRST_NAME, @LAST_NAME, @DEPT, @LEVEL, @CGPA, @MODEOFENTRY)", sqlCon);

    Try this.


    Krishnakumar S

    • Proposed as answer by Olaf HelperMVP Thursday, November 7, 2013 9:40 AM
    • Marked as answer by jerofad Thursday, November 7, 2013 10:09 AM
    Thursday, November 7, 2013 9:23 AM
  • thank you so much I didn't notice it.
    Thursday, November 7, 2013 10:10 AM