I am updating DataTime? column value to null in the database using the Stored procedure and able to update null value in SQL using the same stored procedure. I am using the below code in C#:
if(string.isnullorempty(name)) { name= null; } SqlCommand cmd = new SqlCommand(); cmd = new SqlCommand("procsavedata", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@UserID", UserId); cmd.Parameters.AddWithValue("@Created", (datePicker1.Text.Trim() == string.Empty) ? (DateTime?)null : datePicker1.Value.Date); cmd.Parameters.AddWithValue("@CreatedBy", name); try { conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } catch { } I am able to update the value if datepicker1 and name values exist but if datepicker or name value is null then it is not updating the value in database.
catchblock can hide errors thrown in thetryblock. Try to handle and log exception in thecatchblock. May be it will give you the reason of the problem.(object)DBNull.Value, not(DateTime?)null.null.