1

I am trying to store a null value into a DateTime column. My code is like below:

protected void btnInsert_Click(object sender, EventArgs e)
{
    try
    {
        ChangeModule cm = new ChangeModule();
        cm.CR_REF_NO = txtCRRefNumber.Text.Trim();
        cm.SLA_DELIVERY_DATE_WAIVER = DateTime.ParseExact(txtSLADeliveryDateWaiver.Text, "dd/MM/yy", null);
        else /*This else part is causing problem since I assigned null*/
             cm.SLA_DELIVERY_DATE_WAIVER = null;
    }
    catch (Exception ex)
    {
       throw ex;
    }
}

Code is working fine as long as a proper date exists in SLA_DELIVERY_DATE_WAIVER, but throwing error when there is the blank textbox.

2
  • 1
    did you try the DateTime? var = null; Commented Sep 12, 2011 at 13:47
  • try out DateTime.TryParseExact() Commented Sep 12, 2011 at 13:48

9 Answers 9

2

You can't set a DateTime property to null, because DateTime is a value type. You'll need to change it to a Nullable<DateTime> (aka DateTime?) property - and then make sure that's handled appropriately in your storage layer. It's unclear what's performing the database interaction at the moment.

You should read up on Nullable Value Types on MSDN.

You'll also need to change your code to actually have an if statement, e.g.

DateTime dt;
if (DateTime.TryParseExact(txtSLADeliveryDateWaiver.Text,
                           "dd/MM/yy", null, DateTimeStyles.None, out dt))
{
    cm.SLA_DELIVERY_DATE_WAIVER = dt;
}
else
{
    cm.SLA_DELIVERY_DATE_WAIVER = null;
}

(You should also get rid of the try/catch block - it's actually harming your code as it'll currently mask the source of exceptions, even aside from cluttering it up.)

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

4 Comments

Thank you very much for your help. While declaring the "ChangeModule" class I have declared SLA_DELIVERY_DATE_WAIVER field as Nullable only.
@Ashok: It's not clear what you mean, but it needs to be of type Nullable<DateTime>.
My problem is that null date is not inserting in the database as I have specified in my post. Showing the following error: Now, as well as before also: Procedure or function 'usp_SaveECR' expects parameter '@SLA_DELIVERY_DATE_WAIVER', which was not supplied.
@Ashok: Well that's going to be part of the code you haven't shown. You've only shown the UI code - not the code which is talking to the database.
0

If you make SLA_DELIVERY_DATE_WAIVER a nullable DateTime then you can do this:

 protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            ChangeModule cm = new ChangeModule();
            cm.CR_REF_NO = txtCRRefNumber.Text.Trim();
            cm.SLA_DELIVERY_DATE_WAIVER = String.IsNullOrEmpty(txtSLADeliveryDateWaiver.Text)? null : DateTime.ParseExact(txtSLADeliveryDateWaiver.Text, "dd/MM/yy", null);

        }
        catch (Exception ex)
        {
           throw ex;
        }
    }

2 Comments

Thank you for your reply. I got this following error with the solution you have provided: Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'
Sorry, I don't think I made myself clear enough. My code example requires SLA_DELIVERY_DATE_WAIVER to be made nullable. It looks like a lot of people are saying the same thing. Basically, in the ChangeModule class you have to change the line that says DateTime SLA_DELIVERY_DATE_WAIVER to DateTime? SLA_DELIVERY_DATE_WAIVER. This can have big repercussions in your app, though, so do this carefully and with lots of testing. Good luck!
0

DateTime can't have null value but, adding question mark '?' in front or DateTime data type while declaring makes it nullable, something like this:

DateTime? nullableDateTime = null;

Comments

0

DateTime is not Nullable per se, you need to specify it as a Nullable, as already noted.

So

DateTime SLA_DELIVERY_DATE_WAIVER { get; set; }

wont work, you need to do

DateTime? SLA_DELIVERY_DATE_WAIVER { get; set; }

or

Nullable<DateTime> SLA_DELIVERY_DATE_WAIVER { get; set; }

Are you storing this to a database? You need the column to be nullable too then.

2 Comments

Yeah, I have done like this already. My problem is with the stored procedure insert statement.
@Ashok Wouldn't harm if you would show that proc and the exact exception text in your post.
0

You got exception when there is the blank textbox because DateTime.ParseExact() thrown an FormatException when string to parse is an empty string. Use DateTime.TryParseExact() instead.

From your question it looks like cm.SLA_DELIVERY_DATE_WAIVER is DateTime, change its type to DateTime? so in case of empty textbox: - TryParseExact() will not fail - cm.SLA_DELIVERY_DATE_WAIVER will be able to store NULL value

Comments

0

It is a problem to send null values into date-parameters inside the sql server stored procedires. Hence, whenever there is a need to pass null date from front-end application, pass some min. date like '1/1/1753' for example.

Inside SQL Server stored procedure, check for the null date like below:

IF DATEDIFF(DD, @DATE, '1/1/1753') = 0
    SET @DATE = NULL

Now, you can use @DATE inside SQL Server stored procedure however you like.

Comments

0

Modifying the stored procedure works, but I think its a bit sloppy.

You can handle it in code, this work for me:

            DateTime? myDate;

        if (TextBoxWithDate.Text != "")
        {
            myDate = DateTime.Parse(TextBoxWithDate.Text);
        }
        else
        {
            myDate = null;
        }

Make myDate DateTime type but nullable, if the value from the text box is null, make myDate null and send it to the stored procedure.

Comments

0
if (dtProcessStart.Text.Trim() == "")
{
    cmd.Parameters.Add("@ProcessStartDttm", SqlDbType.DateTime).Value = DBNull.Value;
}

1 Comment

try this, while passing parameter to store procedure.. DBNULL.Value
0
try
{
    ChangeModule cm = new ChangeModule();
    cm.CR_REF_NO = txtCRRefNumber.Text.Trim();
    cm.SLA_DELIVERY_DATE_WAIVER = DateTime.ParseExact(txtSLADeliveryDateWaiver.Text, "dd/MM/yy", null);
    else
         cm.SLA_DELIVERY_DATE_WAIVER = DBNULL.Value;
}
catch (Exception ex)
{
   throw ex;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.