Results 1 to 12 of 12

Thread: Null database fields displaying as 01/00/1900 *RESOLVED *

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Null database fields displaying as 01/00/1900 *RESOLVED *

    I have a databoud grid where I am formatting the date with the following
    {0:dd/mm/yyyy}

    When the date is null in the database it is displaying
    01/00/1900 as the date. How can I suppress this.

    Using VB.Net
    Last edited by davidrobin; Jun 24th, 2004 at 07:48 AM.


    Things I do when I am bored: DotNetable

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    You first have to check for it against 1/1/1900 and if the date is 1/1/1900 don't display it..... It seems stupid, but for some reason MS decided that when you assign A NULL to a data it would be 1/1/1900.... *shakes head*

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    for all the technically complicated achievements Microsoft make they sometimes seem to forget the simple things to make life better.


    Things I do when I am bored: DotNetable

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    This is because NULL is nothing, zilch, blah...

    So, how would you ever be able to format a Null value? The answer is you can't. So MS does the next best thing and gives you a MinDate.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    That's like saying that you can't store a NULL value for an integer, so we should get 0 instead. Or an empty string for a varchar. It's not a format issue, it's about having (or not having) the correct data. Let's say I have an import process, and one of the data peices that comes in is a date field, but it's optional. I should be able to stuff a NULL value in there (which oddly enough, I can do w/o errors) and then retrieve it as such. Naturaly I'd wrap a IS NULL around it to make sure I don't do anything stupid with it.

    It's a silly limitation in my opinion. But, it's not going to change, so one has to work around it. Still doesn't make it right.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by techgnome
    That's like saying that you can't store a NULL value for an integer, so we should get 0 instead. Or an empty string for a varchar. It's not a format issue, it's about having (or not having) the correct data. Let's say I have an import process, and one of the data peices that comes in is a date field, but it's optional. I should be able to stuff a NULL value in there (which oddly enough, I can do w/o errors) and then retrieve it as such. Naturaly I'd wrap a IS NULL around it to make sure I don't do anything stupid with it.

    It's a silly limitation in my opinion. But, it's not going to change, so one has to work around it. Still doesn't make it right.

    TG
    You can't store a null value for an integer, you do get zero...guess I don't understand... (unless you are refering to sql, which in that case you can)

    When you retrieve the value from the database, it is null, but if you assign that null value to a datetime type, you have to convert it to something other than null because the datetime structure doesn't support being null.

    Next version of the framework (2.0) will have support for nullable value types. I think that is going to solve some problems. But right now, you have to check for null before trying to do a format on it, otherwise you get a min date.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    When binding data to controls with ADO if a field had a null value then the cell (suppose we were using a datagrid) would be blank, no value as the field held no value.
    Now when you bind data to a datagrid in .NET as hellswraith mentioned you get the minimum values.
    This causes more work, it would be nice if you had a choice whether you wanted to use the minimum value.


    Things I do when I am bored: DotNetable

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Originally posted by techgnome
    You first have to check for it against 1/1/1900 and if the date is 1/1/1900 don't display it..... It seems stupid, but for some reason MS decided that when you assign A NULL to a data it would be 1/1/1900.... *shakes head*
    TG
    So if I have a datagrid that is bound to a dataset how do I intercept the binding and check the value of the date?


    Things I do when I am bored: DotNetable

  9. #9
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Call a public method that takes the value as a argument. Basically take where the field is specified in the HTML, and call a method that returns a string and takes in a object. Inside the method do the deed.

  10. #10
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    are we talking System.Data.SqlTypes.SqlDateTime or System.DateTime?
    Magiaus

    If I helped give me some points.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Originally posted by hellswraith
    Basically take where the field is specified in the HTML, and call a method that returns a string and takes in a object. Inside the method do the deed.
    It isn't specified in the HTML

    are we talking System.Data.SqlTypes.SqlDateTime or System.DateTime?
    That is a good question, I would say it is SQLDateTime type.


    This is the code I am using to bind the datagrid.
    VB Code:
    1. Private Function PopulateGrid() As DataSet
    2.  
    3.         Dim strSQL As String
    4.  
    5.         strsql="blah blah"
    6.  
    7.         Dim dadData As New SqlDataAdapter(strSQL, clsLib.Connection)
    8.         Dim dstData As New DataSet
    9.         Try
    10.             dadData.Fill(dstData)
    11.             PopulateGrid = dstData
    12.  
    13.         Catch ex As SqlException
    14.             clsLib.HandleErr(ex)
    15.         End Try
    16.     End Function
    At what point would I format the date?


    Things I do when I am bored: DotNetable

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I'm a little bit embarassed because I have found the problem in my SQL expression.

    I have a union so in making sure both queries returned the same data type fields I used
    Code:
    '' as start_date
    and this is interpreted as 01/01/1900 00:00:000 when the query returns.
    What I now have is
    Code:
    null as start_date
    . So now when the data is bound to the datagrid where the field data is null no date is displayed.
    Of course this was always working correctly I had confused myself and lost the plot with my SQL query.


    Things I do when I am bored: DotNetable

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