How to set DateTime variable to Null in C#?

Discussion in 'ASP .Net' started by =?Utf-8?B?UmV6YSBTb2xvdWtp?=, Jun 29, 2006.

  1. Hello,

    I have a system where it gets its data from a file that is provided
    periodically. There are cases where many values such as dates are blank.

    Considering DateTime variable doesn't accept null, and SQL Server doesn't
    accept DateTime.MinValue...

    What is the solution to deal with these kind of cases? I probably could
    create some work around to send some awkword value that SQL accept and then
    treat it from there, but to me there should be some other practical solution
    for this!??
    (btw: my company hasn't updated to 2.0 yet)

    Thanks for your response in advance,
    Reza
     
    =?Utf-8?B?UmV6YSBTb2xvdWtp?=, Jun 29, 2006
    #1
    1. Advertisements

  2. =?Utf-8?B?UmV6YSBTb2xvdWtp?=

    Teemu Keiski Guest

    Hi,

    DateTime is a value type, it cannot represent null (or DBNull)

    simple way is just to set it to DateTime.MinValue and when setting values
    for stored proc parameters, check for DateTime.MinValue, and when that's the
    value send null to the database (set DBNull.Value to be param value)

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "Reza Solouki" <> wrote in message
    news:...
    > Hello,
    >
    > I have a system where it gets its data from a file that is provided
    > periodically. There are cases where many values such as dates are blank.
    >
    > Considering DateTime variable doesn't accept null, and SQL Server doesn't
    > accept DateTime.MinValue...
    >
    > What is the solution to deal with these kind of cases? I probably could
    > create some work around to send some awkword value that SQL accept and
    > then
    > treat it from there, but to me there should be some other practical
    > solution
    > for this!??
    > (btw: my company hasn't updated to 2.0 yet)
    >
    > Thanks for your response in advance,
    > Reza
     
    Teemu Keiski, Jun 29, 2006
    #2
    1. Advertisements

  3. Reza,

    Just replace nulls with something suitable for your code when you read, and
    do the opposite when you write. That's what I am doing and I don't think
    there is anything more elegant.

    Eliyahu

    "Reza Solouki" <> wrote in message
    news:...
    > Hello,
    >
    > I have a system where it gets its data from a file that is provided
    > periodically. There are cases where many values such as dates are blank.
    >
    > Considering DateTime variable doesn't accept null, and SQL Server doesn't
    > accept DateTime.MinValue...
    >
    > What is the solution to deal with these kind of cases? I probably could
    > create some work around to send some awkword value that SQL accept and
    > then
    > treat it from there, but to me there should be some other practical
    > solution
    > for this!??
    > (btw: my company hasn't updated to 2.0 yet)
    >
    > Thanks for your response in advance,
    > Reza
     
    Eliyahu Goldin, Jun 29, 2006
    #3
  4. =?Utf-8?B?UmV6YSBTb2xvdWtp?=

    Mark Rae Guest

    "Reza Solouki" <> wrote in message
    news:...

    Prior to v2, I used to use a separate class for this. Basically, it
    simulated the nullable datatype functionality built into v2. I.e. instead of
    declaring a DateTime variable / object / class (whatever you want to call
    it), I'd declare a NullableDateTime variable, which has a Value property, a
    HasValue property etc.

    I can let you have a copy of it if you like...

    > (btw: my company hasn't updated to 2.0 yet)


    As soon as you upgrade, you'll be able to do this:

    DateTime? dtmTest = null;

    if (dtmTest.HasValue)
    {
    // write the value into the database
    }
    else
    {
    // write a NULL into the database
    }
     
    Mark Rae, Jun 29, 2006
    #4
  5. =?Utf-8?B?UmV6YSBTb2xvdWtp?=

    cronynaval

    Joined:
    Nov 20, 2014
    Messages:
    1
    Likes Received:
    0
    By default DateTime is not nullable because it is a Value Type, using the nullable operator introduced in C# 2, you can achieve this.
    Using a question mark (?) after the type or using the generic style Nullable.
    Nullable < DateTime > nullDateTime;
    or
    DateTime? nullDateTime = null;

    Crony
     
    Last edited by a moderator: Nov 21, 2014
    cronynaval, Nov 20, 2014
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Replies:
    5
    Views:
    31,021
    Mike Schilling
    Mar 29, 2006
  2. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    1,130
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  3. mp
    Replies:
    1
    Views:
    725
    John Machin
    Jul 28, 2006
  4. moonhk
    Replies:
    5
    Views:
    852
    moonhk
    Oct 11, 2006
  5. Replies:
    2
    Views:
    1,111
    M.-A. Lemburg
    Jan 6, 2009
  6. NiklasRTZ

    datetime.datetime. or datetime. ?

    NiklasRTZ, Oct 8, 2009, in forum: Python
    Replies:
    10
    Views:
    820
    niklasr
    Oct 11, 2009
  7. Baiju K U

    Set a datetime field to Null

    Baiju K U, Oct 15, 2003, in forum: ASP General
    Replies:
    1
    Views:
    360
    Ray at
    Oct 15, 2003
Loading...