Results 1 to 3 of 3

Thread: [RESOLVED] What approach for passing null value to date field?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Resolved [RESOLVED] What approach for passing null value to date field?

    hi all,

    i'm not 100% sure about sql server but i think it doesn't accept null value for date type columns but in oracle, a date column can accept a null value.

    and since i'm using oracle, this is causing some problems because in my form, a textbox item bound to a date column will not accept a null value.

    reading thru some threads, it would seem that it is not possible to pass a null value to a date type in .Net.

    but i came across a thread (i can't seem to put the URL here).

    i did try to use the approach where

    Code:
    Dim _NullData As Nullable(Of DateTime) = Nothing
    but when saving the value to the database, i see 01/01/01 on the column in it. still not saving null value to my database but it seemed to save a default value.

    i'd appreciate any help.

    thanks

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: What approach for passing null value to date field?

    Hehe. I just answered this at another forum about 30 seconds ago. I'm not stalking you, I swear. For others' sake, here's my answer:
    Null in VB terms and null in database terms are different things. The fact that your column contains date/time values is completely irrelevant. If you want to assign null to ANY column of ANY data type in ANY database using ADO.NET then you do it in exactly the same way: you use an instance of the DBNull class, which you get from the Shared Value property, e.g.
    Code:
    'Set a field to null in a DataRow.
    myDataRow("AnyColumnAtAll") = DBNull.Value
    
    'Set a parameter to null in an OracleCommand.
    myCommand.Parameters.AddWithValue("pAnyParameterAtAll", DBNull.Value)
    The same goes for testing data that you retrieve from the database. You can either test whether a field value is equal to DBNull.Value or whether its type is DBNull:
    Code:
    If myDataRow("SomeColumn") Is DBNull.Value Then
    Code:
    If TypeOf myDataRow("SomeColumn") Is DBNull Then
    The DataRow class and all DataReader classes also have methods to test a field for null. One's named IsNull and the other is named IsDBNull. I can never remember which is which but they do exactly the same thing so it doesn't really matter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: What approach for passing null value to date field?

    lolz... i read your answer on the other thread first.

    always a pleasure.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width