Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi , i need using C# and SQL for using Insert,delete,update,search ... which how to save value textbox into sql ? i searched alot on google and tried by this code which is same as in many website for insert first but it points an error on cmd.ExecuteNonQuery(); which says SQLEXCEPTION was unhandled (Cannot insert explicit value for identity column in table 'book' when IDENTITY_INSERT is set to OFF.) ... may u help me to solve this error ... i need it very much.

many thanx


C#
private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source =PERSIAN-PC\\SQLEXPRESS;Initial catalog=library;Integrated Security=True");
            
    SqlCommand cmd = new SqlCommand("INSERT INTO book (ide, name, year) VALUES (@a,@b,@c)", con);
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@a", textBox1.Text);
    cmd.Parameters.AddWithValue("@b", textBox2.Text);
    cmd.Parameters.AddWithValue("@c", textBox3.Text);
    con.Open();
    cmd.ExecuteNonQuery(); // here is the error????
    con.Close();
}
Posted
Updated 9-Sep-12 3:59am
v2
Comments
[no name] 9-Sep-12 9:57am    
The error is very clear, either turn IDENTITY_INSERT on or stop trying to insert a value into the identity column.
lida zar 9-Sep-12 10:15am    
thanx
[no name] 9-Sep-12 10:16am    
either turn IDENTITY_INSERT on or stop trying to insert a value into the identity column
lida zar 9-Sep-12 10:02am    
how to solve this ???

(Cannot insert explicit value for identity column in table 'book' when IDENTITY_INSERT is set to OFF.)
It looks like your ID column is set as IDENTITY column in your database.
2 options:
1. Either set this column to allow having values inserted by query OR
2. Don't pass/try to insert ID value. It will automatically get filled when you insert a new book.

Identity columns are generally primary key column and are set to self increment with every insert - this helps in maintaining a unqiue automatic ID generation. I would suggest to follow option 2 and don't pass ID at all. Let DB handle it by itself.

Do:
C#
SqlCommand cmd = new SqlCommand("INSERT INTO book (name, year) VALUES (@b,@c)", con);
cmd.Parameters.AddWithValue("@b", textBox2.Text);
cmd.Parameters.AddWithValue("@c", textBox3.Text);
 
Share this answer
 
Comments
[no name] 9-Sep-12 10:24am    
+5Good description!
When you created the Book table you have created the ide column as IDENTIY column.
You can do any if the below two things

1.Recreate the book table without the identity keyword for ide column
2.Modify the book table to accept insert for identity column using the below statement
SET IDENTITY_INSERT book ON
 
Share this answer
 
In order to insert into a table containing an identity column Set IDENTITY INSERT ''tablename'' On.

http://sqlserverplanet.com/sql-server/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity_insert-is-set-to-off[^]
 
Share this answer
 
v2

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