Identity fields being null

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")
 
G

Guest

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.
--------------------------
 
P

Patrice

Does it work when you run this SQL statement directly against your DB ?

Patrice
 
R

Roger Webb

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!
 
R

Roger Webb

No, it did not. This is what led me to look at the table closer. See my
response to Brian Brown's Post.


Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top