none
SQL Connection With Parameter RRS feed

  • Question

  • I am getting this exception : Incorrect syntax near ')'.

    Here is the code:

    int id = 0;

                SqlConnection sc = new SqlConnection(@"Data Source= COOL-PC\EXPRESS;Initial Catalog= Master ;Integrated Security=SSPI;Trusted_Connection=True;");

                sc.Open();
                SqlCommand sqlcommand = new SqlCommand("INSERT INTO Teacher (@Teacher_ID,@Name,@Father_Name,@Religion, @Caste,@DOB ,@Gender,@CNIC,@Salary,@Permanent_Address,@Recent_Address,@Email,@Mobile_Number,@Home_Number,@Matric_Address,@Intermediat_Address,@Graduation_Address)", sc);////,@Admission_Challan_No,@Challan_Form_Dated,Fee_Paid_On,@Date_of_Admission");
                sqlcommand.Parameters.Add("@Teacher_ID", SqlDbType.Int).Value = id;
                sqlcommand.Parameters.Add("@Name", SqlDbType.VarChar, 100).Value = txtTeacherName.Text;
                sqlcommand.Parameters.Add("@Father_Name", SqlDbType.VarChar, 100).Value = txtTeacherFatherName.Text;
                sqlcommand.Parameters.Add("@Religion", SqlDbType.VarChar, 100).Value = txtTeacherReligion.Text;
                sqlcommand.Parameters.Add("@Caste", SqlDbType.VarChar, 30).Value = txtTeacherCaste.Text;
                sqlcommand.Parameters.Add("@DOB", SqlDbType.Date).Value = Convert.ToDateTime(dtpTeacherDateOfBirth.Value.Date);
                sqlcommand.Parameters.Add("@Gender", SqlDbType.VarChar, 30).Value = cbTeacherGender.SelectedItem.ToString();
                sqlcommand.Parameters.Add("@CNIC", SqlDbType.VarChar, 30).Value = txtTeacherNICNo.Text;
                sqlcommand.Parameters.Add("@Salary", SqlDbType.VarChar,30).Value = txtTeacherSalary.Text;
                
                sqlcommand.Parameters.Add("@Permanent_Address", SqlDbType.VarChar, 30).Value = txtTeacherPermanentAddress.Text;
                sqlcommand.Parameters.Add("@Recent_Address", SqlDbType.VarChar, 30).Value = txtTeacherRecentAddress.Text;
                sqlcommand.Parameters.Add("@Email", SqlDbType.VarChar, 30).Value = txtTeacherEmail.Text;
                sqlcommand.Parameters.Add("@Mobile_Number", SqlDbType.VarChar, 30).Value = txtTeacherMobileNo.Text;
                sqlcommand.Parameters.Add("@Home_Number", SqlDbType.VarChar, 30).Value = txtTeacherHomeNo.Text;
                sqlcommand.Parameters.Add("@Matric_Address", SqlDbType.VarChar, 30).Value = txtTeacherMatric.Text;
                sqlcommand.Parameters.Add("@Intermediat_Address", SqlDbType.VarChar, 30).Value = txtTeacherIntermediate.Text;
                sqlcommand.Parameters.Add("@Graduation_Address", SqlDbType.VarChar, 30).Value = txtTeacherGraduation.Text;
         
                id++;

                sqlcommand.ExecuteNonQuery();  //error occours here
                sc.Close();


    Asia

    • Moved by Bob Beauchemin Saturday, November 24, 2012 9:18 AM Moved to the forum for SqlClient client-side questions (From:.NET Framework inside SQL Server)
    Saturday, November 24, 2012 6:48 AM

Answers

  • Hello Asia,

    Your code is not really good readable, but on a first shot you INSERT statement seems to be incorrect, the key word "VALUES" is missing =>

    INSERT INTO Teacher VALUES
    (@Teacher_ID,@Name,@Father_Name,@Religion, @Caste,@DOB ,@Gender,@CNIC,@Salary,
    @Permanent_Address,@Recent_Address,@Email,@Mobile_Number,@Home_Number,@Matric_Address,
    @Intermediat_Address,@Graduation_Address)

    And you should explicit list the field name where you want to insert the value:

    INSERT INTO Teacher
    (Teacher_id, Name. Father_Name, ....)
    VALUES
    (@Teacher_ID,@Name,@Father_Name,...)

    Otherwise it will fail/will be buggy when you redesign your table

    Olaf Helper

    Blog Xing

    • Marked as answer by Inayat Ullah Saturday, November 24, 2012 7:42 AM
    Saturday, November 24, 2012 6:57 AM

All replies

  • Hello Asia,

    Your code is not really good readable, but on a first shot you INSERT statement seems to be incorrect, the key word "VALUES" is missing =>

    INSERT INTO Teacher VALUES
    (@Teacher_ID,@Name,@Father_Name,@Religion, @Caste,@DOB ,@Gender,@CNIC,@Salary,
    @Permanent_Address,@Recent_Address,@Email,@Mobile_Number,@Home_Number,@Matric_Address,
    @Intermediat_Address,@Graduation_Address)

    And you should explicit list the field name where you want to insert the value:

    INSERT INTO Teacher
    (Teacher_id, Name. Father_Name, ....)
    VALUES
    (@Teacher_ID,@Name,@Father_Name,...)

    Otherwise it will fail/will be buggy when you redesign your table

    Olaf Helper

    Blog Xing

    • Marked as answer by Inayat Ullah Saturday, November 24, 2012 7:42 AM
    Saturday, November 24, 2012 6:57 AM
  • Thanks So so much Olaf

    Asia

    Saturday, November 24, 2012 7:41 AM