Iam trying to insert some data into a SQL Compact Edition table using parameter. My table allows null to be inserted but when I run my code below I can not get it working because one of these paramerters is going to be null sometimes and I would like to insert null into that column when the paramerter is null.
What how can I fix this?
string strConn = Properties.Settings.Default.SqlConnectionString;
using (SqlCeConnection conn = new SqlCeConnection(strConn))
{
conn.Open();
using (SqlCeCommand cmd = new SqlCeCommand("insert into table(ID, Name, Adress) values (@parm1, @parm2, @param3)", conn))
{
cmd.Parameters.AddWithValue("@parm1", ID);
cmd.Parameters.AddWithValue("@parm2", Name);
cmd.Parameters.AddWithValue("@parm3", Adress);
cmd.ExecuteNonQuery();
}
}
I think you can pass DBNull.Value
as your value when it is null:
cmd.Parameters.AddWithValue("@parm2", (Name as object) ?? DBNull.Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With