Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem with DateTime and NULL columns in ADO.NET
Message
From
17/05/2005 21:21:21
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Environment versions
Environment:
C# 1.1
Database:
MS SQL Server
Miscellaneous
Thread ID:
01015071
Message ID:
01015164
Views:
25
Right. In SQL Server, the DateTime column is NULL. This gets sent back through ADO.NET as a DBNull.Value in your datetime column, but as you saw, you can't assign a DBNull.Value to a variable and that's why you need to convert it. (It does need to be an explicit conversion ... we both are using C#). You *do* know that you can do that assignment in fewer steps than what you posted, right?
DateTime dateTime = (DateTime)SqlDateTime.Null;
Anyway, I see that you said that you didn't need to send anything back to the database, but if you did and if you set the DateTime column in your DataTable to SqlDateTime.Null and sent it back to SQL server, then you wouldn't see NULL in SQL Server anymore it would now be '1/1/1900 00:00:00'.

~~Bonnie


>I think the value is actually stored as null, but if you do an explicit conversion from SqlDateTime to DateTime, the value gets converted to "1/1/1900 00:00:00". I tried this, and that is what appears to be happening:
>
>DateTime dateTime;
>SqlDateTime sqlDateTime;
>
>sqlDateTime = SqlDateTime.Null;
>
>dateTime = (DateTime)sqlDateTime;
>
>I was unable to do an implicit conversion, but maybe that is what is going on with your and Kevin's code. Are you using C# or VB?
>
>>You *do* realize that System.Data.SqlTypes.SqlDateTime.Null is actually "1/1/1900 00:00:00", right?
>>
>>~~Bonnie
>>
>>
>>
>>>I had to Google it to find the answer, and after looking at it, I think I am going to leave it the way it was. Serves me right for trying to be consistent.
>>>
>>>I do appreciate you and Kevin looking at it. Sometimes (ok, most of the time) .NET makes me feel stupid.
>>>
>>>>Good going, Chris!! I guess my head wasn't screwed on correctly this afternoon ... dunno how I missed this! You may give Kevin and I 50 lashes!! <g>
>>>>
>>>>~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform