Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem with DateTime and NULL columns in ADO.NET
Message
 
To
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:
01015168
Views:
32
Yes, I realized I could use:
DateTime dateTime = (DateTime)SqlDateTime.Null;
I was trying to show that a variable of type SqlDateTime could hold a value of null, while a variable of type DateTime could not. When I did the explicit conversion from SqlDateTime to DateTime, the DateTime variable had no way to represent null, so it was converted to '1/1/1900 00:00:00'. But you're saying that the SqlDateTime variable isn't actually null, or if I try to send it back to the database, it will be '1/1/1900 00:00:00'? Hmmm... interesting. And scary :-).


>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'.
Chris McCandless
Red Sky Software
Previous
Reply
Map
View

Click here to load this message in the networking platform