Identity fields being null

  • Thread starter Thread starter Roger Webb
  • Start date Start date
R

Roger Webb

Hey All,

I have the code below to run a built up SQL string. Column1 is a Identity
Column, and Column2 is a varchar(10). The sqlString below gives me an
System.Data.SqlException Exception that says, "Cannot insert the value NULL
into column 'Column1', table 'TestTable';column does not allow nulls.
Insert fails." when the sqlCommand.ExecuteNonQuery() gets run.

Obviously since its an Identity field, it cant be null; however, if I give
it a value... it doesnt run the identity value into the field.

Anyone have any ideas? I didnt see any previous threads about this.

- Roger
----------------------------------------
SqlConnection sqlConnection = new SqlConnection(TheConnectionString);
SqlCommand sqlCommand = new SqlCommand(sqlString,sqlConnection);
sqlCommand.Connection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();

Where sqlString looks like
INSERT INTO TABLE1 (Column2) VALUES ("Hey")
 
Hi Roger,

Are you sure that you have Column1 set up as an identity? Or is it set up as
a primary key only? You should have it set to both to get the behavior that
you would like. If this is not it can you script your table and post it here
so the group may help you better.

I hope this helps.
--------------------------
 
Yeah.. it did. My script to create the database had the IDENTITY(1,1) in
it, however, my atual table on the DB did not. Wierd!
 
No, it did not. This is what led me to look at the table closer. See my
response to Brian Brown's Post.


Thanks!
 
Back
Top