3

My code:

SqlCeConnection sql = new SqlCeConnection(@"Data Source=C:\DB.sdf");
sql.Open();
cmd = new SqlCeCommand("INSERT INTO xxx(aaa) VALUES(@aaa)", sql);
String param = null;  //doesn't work
//String param = "blah" //works
cmd.Parameters.AddWithValue("@aaa", param);
cmd.ExecuteNonQuery(); //(1)
sql.Close();

(1) throws an exception when param is null. Database allows value in collumn aaa to be null. How can I insert null into table xxx ?

Exception:
Parameterized query 'INSERT INTO xxx(aaa) VALUES(@aaa)' expects a parameter value which was not supplied.

2 Answers 2

4

Try using DBNull.Value instead:

var param = DBNull.Value;
Sign up to request clarification or add additional context in comments.

Comments

3

Use

var param = DBNull.Value;

null isn't the same thing as DBNull

1 Comment

Can DBNull.Value be cast to String?

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.