Aman Gill

Aman Gill

  • 2.1k
  • 13
  • 147

I'm getting error Must declare the scalar variable @schoolName

Jul 21 2021 11:11 AM

I keep getting the error message System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@schoolName".' - I have a Gridview that captures what the user inputs into texts boxes and submits them to the database on a click of a button. When the user clicks the button I am also trying to store the current user logged in, into the databse. Can someone help me as to why this message keeps occurring? 

Here is the C# code for insertion:

public bool insert (SchoolClass c)
            bool isSuccess = false;
            SqlConnection conn = new SqlConnection(mydbconnection);
            DataTable dt = new DataTable();
                string sql = "INSERT INTO student_info (schoolCode, shoolName, schoolOperater, district, taluka, uc, village, nearFamousPlace, studentName, GR, fatherName, CNIC, religion, gender, DOB_date, DOB_month, DOB_year, Address, DOA_date, DOA_month, DOA_year, classAdmitted, currentClass, section, sift, lastSchool, reasonToLeave, parentContact, emergency, DOS_date, DOS_month, DOS_year ) VALUES (@schoolCode, @shoolName, @schoolOperater, @district, @taluka, @uc, @village, @nearFamousPlace, @studentName, @GR, @fatherName, @CNIC, @religion, @gender, @DOB_date, @DOB_month, @DOB_year, @Address, @DOA_date, @DOA_month, @DOA_year, @classAdmitted, @currentClass, @section, @sift, @lastSchool, @reasonToLeave, @parentContact, @emergency, @DOS_date, @DOS_month, @DOS_year)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@schoolCode", c.schoolCode);
                cmd.Parameters.AddWithValue("@schoolName", c.schoolName);
                cmd.Parameters.AddWithValue("@schoolOperator", c.schoolOperator);


Answers (2)