Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,I have a problem in my program when I want to insert data into the db.This is my code:
public void SaveData(object param)
      {
          string connstring = null;
          connstring = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\user0909\\Documents\\AttendanceStudents.mdf;Integrated Security=True;Connect Timeout=30";
          try
          {
              using (SqlConnection con = new SqlConnection(connstring))
              {


                  string sql = "Insert into RegisterStudent(SN,sNr,fName,lName,semester)Values(@sn,@studentNr,@fName,@lName,@semester)";
                  con.Open();
                  using (SqlCommand cmd = new SqlCommand(sql, con))
                  {
                      cmd.Parameters.AddWithValue("@SN", SqlDbType.Int).Value = sn;
                      cmd.Parameters.AddWithValue("@sNr", SqlDbType.Int).Value = studentNr;
                      cmd.Parameters.AddWithValue("@fName", SqlDbType.NVarChar).Value = fName;
                      cmd.Parameters.AddWithValue("@lName", SqlDbType.NVarChar).Value = lName;
                      cmd.Parameters.AddWithValue("@semester", SqlDbType.Int).Value = semester;
                      if(!string.IsNullOrEmpty(sql))
                      {
                          cmd.CommandText = sql;
                          cmd.ExecuteNonQuery();

                      }

                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }


I insert all the values in the textboxes,but I get the error"
Must declare variable @studentNr"

This is my table:
CREATE TABLE [dbo].[RegisterStudent]
(
	[SN] INT NOT NULL PRIMARY KEY, 
    [sNr] INT NOT NULL, 
    [fName] NVARCHAR(MAX) NOT NULL, 
    [lName] NVARCHAR(MAX) NOT NULL, 
    [semester] INT NULL
)


What I have tried:

This is what I have tried:
SqlParameter StudentNR = cmd.Parameters.AddWithValue("@sNr", SqlDbType.Int);

             cmd.Parameters["@sNr"].Value = studentNr;
             if (StudentNR.Value == null)
             {
                 StudentNR.Value = DBNull.Value;
             }

I tried this approach,but I still get the error?What could be the problem?Thank you in advance!
Posted
Updated 2-May-18 1:00am

1 solution

In the SQL string you are using a param name @studentNr but the param you are defining is called @sNr, they have to match

string sql = "Insert into RegisterStudent(SN,sNr,fName,lName,semester)Values(@sn,@sNr,@fName,@lName,@semester)";
 
Share this answer
 
Comments
Daniel Andrei Popescu 2-May-18 7:15am    
Thank you for your response!You are right.I didn't actually knew that they have to match.I knew I have to pass the values from view to VM,but thank you for the lesson.Next time I won't do the same mistake :).Best regards!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900