0
sCnd =  "INSERT INTO .dbo.Table(Col1, Col2, Col3) Values (1,2,3);

using (DbCommand tempCommand2 = CommandFactory.CreateCommand(db))
{
    tempCommand2.CommandText = sCmd;
    tempCommand2.ExecuteNonQuery();
}

Table has also a Col4 that does not allow null.

Error: Cannot insert the value NULL into column Col4

When I execute the sCmd in the Management Studio everything works fine.

Why do I get the exception?

3 Answers 3

2

Table has also a Col4 that does not allow null.

There is your answer. If Management Studio works anyway, it is probably inserting a default value?

Sign up to request clarification or add additional context in comments.

Comments

2

You get the exception because you are not inserting a value for the Col4 column, which does not allow nulls and, I can only assume, does not have a default value.

But it seems like you've asked the wrong question; don't you mean to be asking, why does the query run in SSMS?

I suspect the answer to that question would be buried somewhere in the actual syntax, which you have elected not to include.

Comments

1

If you don't specify a value for a column NULL is taken, unless there is a constraint which says otherwise.

As Col4 doesn't allow NULL and looks like it doesn't have a default value constraint that is why you get the error.

You haven't shown the SQL you are executing in SSMS, but I'd guess, different database (production/development), different table?

1 Comment

You haven't shown the SQL you are executing in SSMS, but I'd guess, different database (production/development), different table? Yes ... :s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.