c# - "Cannot insert explicit value for identity column in table 'tbl Fingerprint' when IDENTITY_INSERT is set to OFF -


[webmethod]         public bool addstudent(student student)         {             bool uploadsuccess = false;             cn.open();             int studentid = 0;             using (sqlcommand com = new sqlcommand("insert 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 = com.executenonquery();                 studentid = (int)com.executescalar();                 cn.close();                 if (i != 0)                      uploadsuccess = true;                  return uploadsuccess;             } 

i'm trying insert data fingerprint table has 4 columns - id (primary key) - studentid (foreign key) linked student table - description - template

but following error keeps coming up. can't turn off identity id want increment automatically. have student table store information. want achieve after entering student details, want copy studentid generated before onto fingerprint table - studentid column. code have shown below.

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 added!");             }             else             {                 messagebox.show("student not added!");             }  using (sqlcommand com = new sqlcommand("insert tblfingerprint (studentid, description, template) values ('" + template.studentid + "' ,'" + template.description + "' ,@template)", cn)) 

this have on web service. gives me error

your first error:

you trying insert studentid seems identity type field (auto-increment), don't have pass in insert statement, sql server generate 1 your.

your second problem is: query not parameterized, using combination of string concatenation , parameters. query , statement should like:

using (sqlcommand com = new sqlcommand(@"insert tblfingerprint (description, template)                                           values (@description, @template)", cn)) {     com.parameters.addwithvalue("@description", template.descriptio);     com.parameters.addwithvalue("@template", template.value); //what ever value     //....rest of code  } 

if have studentid , want update existing record use update statement.

if want manually insert studentid (overriding auto increment id) have use set identity_insert


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -