Handling null DateTime objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manish Jain

    #1

    Handling null DateTime objects?

    Platform : ASP.Net/C#

    void SomeFunction(Da teTime date)
    {
    string text = date.ToString() ; //This crashes if date is null
    }

    I am using a construct similar to above. I want to check if the date is null
    or nor before processing it, but I am not able to figure out the syntax.

    Please suggest my approach.

    I want something like:

    if (date is null)
    {
    text = "";
    }
    else
    {
    text = date.ToString() ;
    }

    Thanks

    Manish


  • Jon Skeet

    #2
    Re: Handling null DateTime objects?

    Manish Jain <manish04@hotma il.com> wrote:[color=blue]
    > Platform : ASP.Net/C#
    >
    > void SomeFunction(Da teTime date)
    > {
    > string text = date.ToString() ; //This crashes if date is null
    > }[/color]

    date can't be null. DateTime is a value type, null is a reference.
    Please come up with a short but complete program to demonstrate your
    problem - see http://www.pobox.com/~skeet/csharp/complete.html for
    details.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Manish Jain

      #3
      Re: Handling null DateTime objects?

      Sorry about the incomplete info. I am using MSPetshop architecture.

      In the SQL Server layer, I get the the data into a Data Model class:
      if (reader.Read())
      {
      ShortTaskInfo item = new ShortTaskInfo() ;
      item.TaskID = reader.GetInt32 (0);
      item.Subject = ( reader.IsDBNull (1) ? "" : reader.GetStrin g(1) );
      item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
      item.DueDate = reader.GetDateT ime(3);
      }

      But since I cannot pass back a null in DateTime, I am passing whatever I get
      from the database. This field is optional, and can be null in database. I am
      not able to handle these null values on my UI layer, and they are causing a
      crash. I do not want to resort to workarrounds like passing
      DateTime.MaxVal ue or DateTime.MinVal ue for null and checking for that on my
      UI.

      Hope I am more clear this time.

      Manish



      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19c664 b7fa913bec98a4c d@news.microsof t.com...[color=blue]
      > Manish Jain <manish04@hotma il.com> wrote:[color=green]
      > > Platform : ASP.Net/C#
      > >
      > > void SomeFunction(Da teTime date)
      > > {
      > > string text = date.ToString() ; //This crashes if date is null
      > > }[/color]
      >
      > date can't be null. DateTime is a value type, null is a reference.
      > Please come up with a short but complete program to demonstrate your
      > problem - see http://www.pobox.com/~skeet/csharp/complete.html for
      > details.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet/
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet

        #4
        Re: Handling null DateTime objects?

        Manish Jain <manish04@hotma il.com> wrote:[color=blue]
        > Sorry about the incomplete info. I am using MSPetshop architecture.
        >
        > In the SQL Server layer, I get the the data into a Data Model class:
        > if (reader.Read())
        > {
        > ShortTaskInfo item = new ShortTaskInfo() ;
        > item.TaskID = reader.GetInt32 (0);
        > item.Subject = ( reader.IsDBNull (1) ? "" : reader.GetStrin g(1) );
        > item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
        > item.DueDate = reader.GetDateT ime(3);
        > }
        >
        > But since I cannot pass back a null in DateTime, I am passing whatever I get
        > from the database. This field is optional, and can be null in database. I am
        > not able to handle these null values on my UI layer, and they are causing a
        > crash. I do not want to resort to workarrounds like passing
        > DateTime.MaxVal ue or DateTime.MinVal ue for null and checking for that on my
        > UI.[/color]

        Well, you could create your own class, eg DateTimeWrapper , which would
        store a DateTime inside it (potentially with conversions between the
        two) - then you could have a null reference to a DateTimeWrapper
        instead.

        I'm still not sure exactly what you *are* doing though - what do you
        want the final effect to be? As I said before, you definitely *don't*
        have a null DateTime reference at the moment, as DateTime isn't a
        reference type. I think you need to have a look at the stack trace from
        the exception and work out exactly why you're getting it - that may
        well give you a good idea about the best way to go about fixing it.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Manish Jain

          #5
          Re: Handling null DateTime objects?

          Thanks, this should work.

          "Jon Skeet" <skeet@pobox.co m> wrote in message
          news:MPG.19c676 ee2b2ebfa098a4c e@news.microsof t.com...[color=blue]
          > Manish Jain <manish04@hotma il.com> wrote:[color=green]
          > > Sorry about the incomplete info. I am using MSPetshop architecture.
          > >
          > > In the SQL Server layer, I get the the data into a Data Model class:
          > > if (reader.Read())
          > > {
          > > ShortTaskInfo item = new ShortTaskInfo() ;
          > > item.TaskID = reader.GetInt32 (0);
          > > item.Subject = ( reader.IsDBNull (1) ? "" : reader.GetStrin g(1) );
          > > item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
          > > item.DueDate = reader.GetDateT ime(3);
          > > }
          > >
          > > But since I cannot pass back a null in DateTime, I am passing whatever I[/color][/color]
          get[color=blue][color=green]
          > > from the database. This field is optional, and can be null in database.[/color][/color]
          I am[color=blue][color=green]
          > > not able to handle these null values on my UI layer, and they are[/color][/color]
          causing a[color=blue][color=green]
          > > crash. I do not want to resort to workarrounds like passing
          > > DateTime.MaxVal ue or DateTime.MinVal ue for null and checking for that on[/color][/color]
          my[color=blue][color=green]
          > > UI.[/color]
          >
          > Well, you could create your own class, eg DateTimeWrapper , which would
          > store a DateTime inside it (potentially with conversions between the
          > two) - then you could have a null reference to a DateTimeWrapper
          > instead.
          >
          > I'm still not sure exactly what you *are* doing though - what do you
          > want the final effect to be? As I said before, you definitely *don't*
          > have a null DateTime reference at the moment, as DateTime isn't a
          > reference type. I think you need to have a look at the stack trace from
          > the exception and work out exactly why you're getting it - that may
          > well give you a good idea about the best way to go about fixing it.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet/
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Ignacio Machin

            #6
            Re: Handling null DateTime objects?

            Hi Manish,

            Why don't you do the same with the DateTime column ? I mean if it's possible
            that it's null on the DB then you should defenitly check it on your code,
            now the BIG question is what to do in that case, I can think of two choises
            : 1- Do as Jon said and write a wrapper class to handle it as you need in
            your particular situation or 2- Set it to a particular value which can work
            as a "null" value in your program ( maybe DateTime.Minval ue ) and later you
            can check for it if ( item.DueDate == DateTime.MinVal ue ) // it was a null
            on the DB.


            Hope this help,

            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation

            "Manish Jain" <manish04@hotma il.com> wrote in message
            news:eEXQVhfdDH A.3088@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Sorry about the incomplete info. I am using MSPetshop architecture.
            >
            > In the SQL Server layer, I get the the data into a Data Model class:
            > if (reader.Read())
            > {
            > ShortTaskInfo item = new ShortTaskInfo() ;
            > item.TaskID = reader.GetInt32 (0);
            > item.Subject = ( reader.IsDBNull (1) ? "" : reader.GetStrin g(1) );
            > item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
            > item.DueDate = reader.GetDateT ime(3);
            > }
            >
            > But since I cannot pass back a null in DateTime, I am passing whatever I[/color]
            get[color=blue]
            > from the database. This field is optional, and can be null in database. I[/color]
            am[color=blue]
            > not able to handle these null values on my UI layer, and they are causing[/color]
            a[color=blue]
            > crash. I do not want to resort to workarrounds like passing
            > DateTime.MaxVal ue or DateTime.MinVal ue for null and checking for that on[/color]
            my[color=blue]
            > UI.
            >
            > Hope I am more clear this time.
            >
            > Manish
            >
            >
            >
            > "Jon Skeet" <skeet@pobox.co m> wrote in message
            > news:MPG.19c664 b7fa913bec98a4c d@news.microsof t.com...[color=green]
            > > Manish Jain <manish04@hotma il.com> wrote:[color=darkred]
            > > > Platform : ASP.Net/C#
            > > >
            > > > void SomeFunction(Da teTime date)
            > > > {
            > > > string text = date.ToString() ; //This crashes if date is null
            > > > }[/color]
            > >
            > > date can't be null. DateTime is a value type, null is a reference.
            > > Please come up with a short but complete program to demonstrate your
            > > problem - see http://www.pobox.com/~skeet/csharp/complete.html for
            > > details.
            > >
            > > --
            > > Jon Skeet - <skeet@pobox.co m>
            > > http://www.pobox.com/~skeet/
            > > If replying to the group, please do not mail me too[/color]
            >
            >[/color]


            Comment

            • james

              #7
              Re: Handling null DateTime objects?

              I second the DateTime.MinVal ue concept. The only problem you need to
              consider
              though is in the GUI, you do not want to display DateTime.MinVal ue (1/1/01)
              so you
              have to handle the events like TextChanged, check for that and display a
              blank.

              I will never understand why MS didn't make DateTime an object class.

              JIM


              "Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote in message
              news:OGyJBtgdDH A.1128@tk2msftn gp13.phx.gbl...[color=blue]
              > Hi Manish,
              >
              > Why don't you do the same with the DateTime column ? I mean if it's[/color]
              possible[color=blue]
              > that it's null on the DB then you should defenitly check it on your code,
              > now the BIG question is what to do in that case, I can think of two[/color]
              choises[color=blue]
              > : 1- Do as Jon said and write a wrapper class to handle it as you need in
              > your particular situation or 2- Set it to a particular value which can[/color]
              work[color=blue]
              > as a "null" value in your program ( maybe DateTime.Minval ue ) and later[/color]
              you[color=blue]
              > can check for it if ( item.DueDate == DateTime.MinVal ue ) // it was a[/color]
              null[color=blue]
              > on the DB.
              >
              >
              > Hope this help,
              >
              > --
              > Ignacio Machin,
              > ignacio.machin AT dot.state.fl.us
              > Florida Department Of Transportation
              >
              > "Manish Jain" <manish04@hotma il.com> wrote in message
              > news:eEXQVhfdDH A.3088@TK2MSFTN GP09.phx.gbl...[color=green]
              > > Sorry about the incomplete info. I am using MSPetshop architecture.
              > >
              > > In the SQL Server layer, I get the the data into a Data Model class:
              > > if (reader.Read())
              > > {
              > > ShortTaskInfo item = new ShortTaskInfo() ;
              > > item.TaskID = reader.GetInt32 (0);
              > > item.Subject = ( reader.IsDBNull (1) ? "" : reader.GetStrin g(1) );
              > > item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
              > > item.DueDate = reader.GetDateT ime(3);
              > > }
              > >
              > > But since I cannot pass back a null in DateTime, I am passing whatever I[/color]
              > get[color=green]
              > > from the database. This field is optional, and can be null in database.[/color][/color]
              I[color=blue]
              > am[color=green]
              > > not able to handle these null values on my UI layer, and they are[/color][/color]
              causing[color=blue]
              > a[color=green]
              > > crash. I do not want to resort to workarrounds like passing
              > > DateTime.MaxVal ue or DateTime.MinVal ue for null and checking for that on[/color]
              > my[color=green]
              > > UI.
              > >
              > > Hope I am more clear this time.
              > >
              > > Manish
              > >
              > >
              > >
              > > "Jon Skeet" <skeet@pobox.co m> wrote in message
              > > news:MPG.19c664 b7fa913bec98a4c d@news.microsof t.com...[color=darkred]
              > > > Manish Jain <manish04@hotma il.com> wrote:
              > > > > Platform : ASP.Net/C#
              > > > >
              > > > > void SomeFunction(Da teTime date)
              > > > > {
              > > > > string text = date.ToString() ; //This crashes if date is[/color][/color][/color]
              null[color=blue][color=green][color=darkred]
              > > > > }
              > > >
              > > > date can't be null. DateTime is a value type, null is a reference.
              > > > Please come up with a short but complete program to demonstrate your
              > > > problem - see http://www.pobox.com/~skeet/csharp/complete.html for
              > > > details.
              > > >
              > > > --
              > > > Jon Skeet - <skeet@pobox.co m>
              > > > http://www.pobox.com/~skeet/
              > > > If replying to the group, please do not mail me too[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Luca Minudel

                #8
                RE: Handling null DateTime objects?

                I'm working on a .NET library that implements a nullable
                DateTime. It's open-source and it can be used in
                commercial closed-source projects without restrictions
                (MIT license).

                The project is NullableTypes.
                Look at http://nullabletypes.sourceforge.net/

                HTH (luKa)
                NullableTypes Project Manager

                Comment

                • Michael Roper

                  #9
                  Re: Handling null DateTime objects?

                  Luca Minudel writes:[color=blue]
                  > The project is NullableTypes.
                  > Look at http://nullabletypes.sourceforge.net/[/color]

                  Is that green-on-black nullable?

                  :)

                  Michael Roper


                  Comment

                  Working...