"right" way to handle null date values?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean Slindee

    "right" way to handle null date values?

    I am looking for the "right" way to handle inserting and presenting null
    date values.
    Public Const c_NullDate As Date = #12:00:00 AM#
    If I set the value of a date variable in an SQL Server insert statement to
    c_NullDate, the insert statement works. When I re-read the row and display
    the inserted date value in a text box, the string "1/1/1900" is displayed.
    If one wanted the textbox to display an empty string, how should either the
    insert to the db be handled, or should code to show a blank string in the
    text box be added?

    Thanks

    Dean Slindee


  • Kevin Yu [MSFT]

    #2
    RE: "right&quo t; way to handle null date values?

    Hi Dean,

    Generally, when you want to assign a null value to the data type, you can
    use DBNull.Value. In database, the field will be marked as <null>. When you
    bind the data source to a textbox, the textbox will display an empty string
    if the value of the field is DBNull.Value.

    If anything is unclear, please feel free to reply to the post.

    Kevin Yu
    =======
    "This posting is provided "AS IS" with no warranties, and confers no
    rights."

    --------------------
    | From: "Dean Slindee" <slindee@mindsp ring.com>
    | Subject: "right" way to handle null date values?
    | Date: Thu, 13 Nov 2003 22:15:17 -0600
    | Lines: 15
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Message-ID: <#n8ipXmqDHA.28 08@TK2MSFTNGP10 .phx.gbl>
    | Newsgroups: microsoft.publi c.dotnet.langua ges.vb
    | NNTP-Posting-Host: 0-1pool36-160.nas14.milwa ukee1.wi.us.da. qwest.net
    63.156.36.160
    | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
    | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:156742
    | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
    |
    | I am looking for the "right" way to handle inserting and presenting null
    | date values.
    | Public Const c_NullDate As Date = #12:00:00 AM#
    | If I set the value of a date variable in an SQL Server insert statement to
    | c_NullDate, the insert statement works. When I re-read the row and
    display
    | the inserted date value in a text box, the string "1/1/1900" is displayed.
    | If one wanted the textbox to display an empty string, how should either
    the
    | insert to the db be handled, or should code to show a blank string in the
    | text box be added?
    |
    | Thanks
    |
    | Dean Slindee
    |
    |
    |

    Comment

    • Dean Slindee

      #3
      Re: &quot;right&quo t; way to handle null date values?

      Value of type 'System.DBNull' cannot be converted to 'Date'.

      is returned by the VS.Net source editor. Still wishing for a way to insert
      a null

      value into a date field and subsequently into a date column in SQLServer.

      Currently inserts '1/1/1900'

      Thanks,

      Dean Slindee

      "Kevin Yu [MSFT]" <v-kevy@online.mic rosoft.com> wrote in message
      news:S7UJTynqDH A.2396@cpmsftng xa06.phx.gbl...[color=blue]
      > Hi Dean,
      >
      > Generally, when you want to assign a null value to the data type, you can
      > use DBNull.Value. In database, the field will be marked as <null>. When[/color]
      you[color=blue]
      > bind the data source to a textbox, the textbox will display an empty[/color]
      string[color=blue]
      > if the value of the field is DBNull.Value.
      >
      > If anything is unclear, please feel free to reply to the post.
      >
      > Kevin Yu
      > =======
      > "This posting is provided "AS IS" with no warranties, and confers no
      > rights."
      >
      > --------------------
      > | From: "Dean Slindee" <slindee@mindsp ring.com>
      > | Subject: "right" way to handle null date values?
      > | Date: Thu, 13 Nov 2003 22:15:17 -0600
      > | Lines: 15
      > | X-Priority: 3
      > | X-MSMail-Priority: Normal
      > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
      > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
      > | Message-ID: <#n8ipXmqDHA.28 08@TK2MSFTNGP10 .phx.gbl>
      > | Newsgroups: microsoft.publi c.dotnet.langua ges.vb
      > | NNTP-Posting-Host: 0-1pool36-160.nas14.milwa ukee1.wi.us.da. qwest.net
      > 63.156.36.160
      > | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
      > | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:156742
      > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
      > |
      > | I am looking for the "right" way to handle inserting and presenting null
      > | date values.
      > | Public Const c_NullDate As Date = #12:00:00 AM#
      > | If I set the value of a date variable in an SQL Server insert statement[/color]
      to[color=blue]
      > | c_NullDate, the insert statement works. When I re-read the row and
      > display
      > | the inserted date value in a text box, the string "1/1/1900" is[/color]
      displayed.[color=blue]
      > | If one wanted the textbox to display an empty string, how should either
      > the
      > | insert to the db be handled, or should code to show a blank string in[/color]
      the[color=blue]
      > | text box be added?
      > |
      > | Thanks
      > |
      > | Dean Slindee
      > |
      > |
      > |
      >[/color]


      Comment

      • Kevin Yu [MSFT]

        #4
        Re: &quot;right&quo t; way to handle null date values?

        Hi Dean,

        Please make sure to use DBNull.Value instead of DBNull. DBNull.Value cannot
        be assigned to a DateTime object. However, it has to be assigned to the
        value of a sqlParameter. Please try the following code.

        SqlParameter p = new SqlParameter("@ ParaName", SqlDbType.DateT ime, 8);
        p.Value = System.DBNull.V alue;

        Kevin Yu
        =======
        "This posting is provided "AS IS" with no warranties, and confers no
        rights."

        --------------------
        | From: "Dean Slindee" <slindee@mindsp ring.com>
        | References: <#n8ipXmqDHA.28 08@TK2MSFTNGP10 .phx.gbl>
        <S7UJTynqDHA.23 96@cpmsftngxa06 .phx.gbl>
        | Subject: Re: "right" way to handle null date values?
        | Date: Fri, 14 Nov 2003 12:36:23 -0600
        | Lines: 72
        | X-Priority: 3
        | X-MSMail-Priority: Normal
        | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
        | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
        | Message-ID: <ObMZ04tqDHA.64 8@TK2MSFTNGP11. phx.gbl>
        | Newsgroups: microsoft.publi c.dotnet.langua ges.vb
        | NNTP-Posting-Host: 0-1pool36-60.nas14.milwau kee1.wi.us.da.q west.net
        63.156.36.60
        | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
        | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:156939
        | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
        |
        | Value of type 'System.DBNull' cannot be converted to 'Date'.
        |
        | is returned by the VS.Net source editor. Still wishing for a way to
        insert
        | a null
        |
        | value into a date field and subsequently into a date column in SQLServer.
        |
        | Currently inserts '1/1/1900'
        |
        | Thanks,
        |
        | Dean Slindee
        |
        | "Kevin Yu [MSFT]" <v-kevy@online.mic rosoft.com> wrote in message
        | news:S7UJTynqDH A.2396@cpmsftng xa06.phx.gbl...
        | > Hi Dean,
        | >
        | > Generally, when you want to assign a null value to the data type, you
        can
        | > use DBNull.Value. In database, the field will be marked as <null>. When
        | you
        | > bind the data source to a textbox, the textbox will display an empty
        | string
        | > if the value of the field is DBNull.Value.
        | >
        | > If anything is unclear, please feel free to reply to the post.
        | >
        | > Kevin Yu
        | > =======
        | > "This posting is provided "AS IS" with no warranties, and confers no
        | > rights."
        | >
        | > --------------------
        | > | From: "Dean Slindee" <slindee@mindsp ring.com>
        | > | Subject: "right" way to handle null date values?
        | > | Date: Thu, 13 Nov 2003 22:15:17 -0600
        | > | Lines: 15
        | > | X-Priority: 3
        | > | X-MSMail-Priority: Normal
        | > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
        | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
        | > | Message-ID: <#n8ipXmqDHA.28 08@TK2MSFTNGP10 .phx.gbl>
        | > | Newsgroups: microsoft.publi c.dotnet.langua ges.vb
        | > | NNTP-Posting-Host: 0-1pool36-160.nas14.milwa ukee1.wi.us.da. qwest.net
        | > 63.156.36.160
        | > | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
        | > | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:156742
        | > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
        | > |
        | > | I am looking for the "right" way to handle inserting and presenting
        null
        | > | date values.
        | > | Public Const c_NullDate As Date = #12:00:00 AM#
        | > | If I set the value of a date variable in an SQL Server insert
        statement
        | to
        | > | c_NullDate, the insert statement works. When I re-read the row and
        | > display
        | > | the inserted date value in a text box, the string "1/1/1900" is
        | displayed.
        | > | If one wanted the textbox to display an empty string, how should
        either
        | > the
        | > | insert to the db be handled, or should code to show a blank string in
        | the
        | > | text box be added?
        | > |
        | > | Thanks
        | > |
        | > | Dean Slindee
        | > |
        | > |
        | > |
        | >
        |
        |
        |

        Comment

        • Kevin Yu [MSFT]

          #5
          Re: &quot;right&quo t; way to handle null date values?

          Hi Dean,

          I'd like to know if this issue has been resolved yet. Is there anything
          that I can help on this? I'm still monitoring on it.

          Kevin Yu
          =======
          "This posting is provided "AS IS" with no warranties, and confers no
          rights."

          Comment

          • Jacky Ott

            #6
            Re: &quot;right&quo t; way to handle null date values?


            Kevin,
            Have you checked the field for DBNULL? Hope this will help you out.







            Kevin Yu [MSFT] wrote:[color=blue]
            > Hi Dean,
            >
            > I'd like to know if this issue has been resolved yet. Is there anything
            > that I can help on this? I'm still monitoring on it.
            >
            > Kevin Yu
            > =======
            > "This posting is provided "AS IS" with no warranties, and confers no
            > rights."
            >[/color]

            Comment

            • Kevin Yu [MSFT]

              #7
              Re: &quot;right&quo t; way to handle null date values?

              Thank you, Jacky. I have checked that.

              Kevin Yu
              =======
              "This posting is provided "AS IS" with no warranties, and confers no
              rights."

              Comment

              Working...