0

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.

5
  • 1
    Empty catch block can hide errors thrown in the try block. Try to handle and log exception in the catch block. May be it will give you the reason of the problem. Commented Sep 9, 2020 at 3:24
  • 1
    (object)DBNull.Value, not (DateTime?)null. Commented Sep 9, 2020 at 3:26
  • Yes, I am using a catch block with an error message. Commented Sep 9, 2020 at 3:26
  • 1
    My guess is that the exception mentions that you forgot to provide @Created? That is because you passed null. Commented Sep 9, 2020 at 3:27
  • @mjwills. Thanks, it is working fine now, accepting null values in database. Commented Sep 9, 2020 at 3:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.