I have an identity column in the DB which is primary key and increments its value by itself. But I have a insert query in asp.net ado.net class like below:
cmd = new SqlCommand("insert into Images(Name,[Image]) VALUES ('Nature',@img)", conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@img", SqlDbType.Image).Value = img;
cmd.ExecuteNonQuery();
However, it throws me exception that it cannot be null.
My idea/goal is, I just want to insert the above two values only and the primary key column should increment by itself internally in DB. I have configured that in DB. However I don’t know how to handle this error or tweak this code. Please help
Make sure the
IDcolumn on yourImagestable is an identity column.For example:
If it is, then step through your code and make sure the parameters you are using are not null;
TextBox1.Textandimg.Lastly, make sure there are no other non-nullable columns on the table.