title: "Cannot insert explicit value for identity column in table 'tbl Fingerprint' when IDENTITY_INSERT is set to OFF
body: <pre><code>[WebMethod]
          public bool AddStudent(Student student)
          {             bool UploadSuccess = false;
              cn.Open();
              int StudentID = 0;
              using (SqlCommand com = new SqlCommand("INSERT into tblStudent (StudentNumber, Name, Surname, DOB, Gender, EmailAddress, Address1, Address2, City, Postcode, Username, Password, Course) values ('" + student.StudentNumber + "' ,'" + student.Name + "' ,'" + student.Surname + "' ,'" + student.DOB + "', '" + student.Gender + "' ,'" + student.EmailAddress + "' ,'" + student.Address1 + "' ,'" + student.Address2 + "' ,'" + student.City + "' ,'" + student.Postcode + "' ,'" + student.Username + "' ,'" + student.Password + "' ,'" + student.Course + "')", cn))
              {
                  int i = com.ExecuteNonQuery();
                  StudentID = (int)com.ExecuteScalar();
                  cn.Close();
                  if (i != 0)
  
                      UploadSuccess = true;
  
                  return UploadSuccess;
              }
  </code></pre>
  
  <p>I'm trying to insert data into fingerprint table which has four columns - ID (primary key) - StudentID (foreign key) linked to student table - description - Template</p>
  
  <p>But the following error keeps coming up. I can't turn off the IDENTITY for ID as I want it to increment automatically. I also have a student table to store information. What I want to achieve is that after entering student details, I want to copy the studentID that was generated before onto the fingerprint table - StudentID column. the code I have for this is shown below.</p>
  
  <pre><code>private void btnSave_Click(object sender, EventArgs e)
          {
              fgrTemplate template = new fgrTemplate();
              template.StudentID = std.StudentID;
              template.Description = fngDes.Text;
              template.Template = m_StoredTemplate;
              if (upload.InsertTemplate(template))
              {
                  MessageBox.Show("Student Successfully Added!");
              }
              else
              {
                  MessageBox.Show("Student Not Successfully Added!");
              }
  
  using (SqlCommand com = new SqlCommand("INSERT INTO tblFingerprint (StudentID, Description, Template) values ('" + template.StudentID + "' ,'" + template.Description + "' ,@Template)", cn))
  </code></pre>
  
  <p>this is what I have on my web service. However it gives me the error</p>