dealing with null and datetime

G

GaryDean

Is there any good way in which to deal with null datetimes. For instance, I
can't seem to find anything that will pass a null value in a datetime
parameter to another method. Convert.dbnull will not work for instance.
Getting nulls back and forth between C# and SQL Server is always
problematic.
 
M

Mark Rae

Is there any good way in which to deal with null datetimes. For instance,
I can't seem to find anything that will pass a null value in a datetime
parameter to another method. Convert.dbnull will not work for instance.
Getting nulls back and forth between C# and SQL Server is always
problematic.

Use a nullable DateTime variable e.g.

DateTime? dtmNullable = null;
 
G

George Ter-Saakov

The problem is that DateTime is a value type. Value type can not be null.
I usually set January 1, 2000 (or any other date) as a null date and check
for it as if you would check for null.

George.
 
G

George Ter-Saakov

Sorry, I did not understand.
What is "nullable DateTime variable"?

And what is "DateTime? dtmNullable = null;"?

Can you please use it in a code maybe?

Thanks
George.
 
M

Mark Rae

Sorry, I did not understand.
What is "nullable DateTime variable"?

It is a DateTime variable which can be null.
And what is "DateTime? dtmNullable = null;"?

It is a piece of C# code which instantiates a DateTime? variable and assigns
it a null value.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

GaryDean said:
Is there any good way in which to deal with null datetimes. For instance, I
can't seem to find anything that will pass a null value in a datetime
parameter to another method. Convert.dbnull will not work for instance.
Getting nulls back and forth between C# and SQL Server is always
problematic.

I wouldn't say that it's problematic, but you always have to write the
code for it.

When you get the value from the database, you first have to check if
it's a null value or not, before you can use the value.

Then you have to decide how you represent a null value in your code. A
null value from the database comes as a DBNull object into your code,
but that is mostly not a practical way to handle it in the code.

For a DateTime value you can use a nullable DateTime to handle the value:

DateTime? createdDate;
if (reader.IsDBNull(0)) {
createdDate = null;
} else {
createdDate = reader.GetDateTime(0);
}

To use the nullable value, you can check createdDate.HasValue to see if
there is a value, and createdDate.Value to get the value.
 
G

George Ter-Saakov

Ha,
I never knew that. I guess it's something new from .NET 2.0
It turned out that even int? is nullable int.

Thanks
George.
 
M

Mark Rae

I never knew that. I guess it's something new from .NET 2.0

That's right. Nullable datatypes and generics are the two new features in v2
which I have found the most useful...
It turned out that even int? is nullable int.

That's also right.
 
G

GaryDean

Yes, that does help get null dates in and out of methods but it seems I
still have to do the following when taking it to a database.

if (DateAcknowledged == null)
{
cmd.Parameters.Add(new SqlParameter("@DateAcknowledged",
Convert.DBNull));
}
else
{
cmd.Parameters.Add(new SqlParameter("@DateAcknowledged",
DateAcknowledged));
}
 
M

Mark Rae

Yes, that does help get null dates in and out of methods but it seems I
still have to do the following when taking it to a database.

if (DateAcknowledged == null)
{
cmd.Parameters.Add(new SqlParameter("@DateAcknowledged",
Convert.DBNull));
}
else
{
cmd.Parameters.Add(new SqlParameter("@DateAcknowledged",
DateAcknowledged));
}

Correct.
 
S

Steven Cheng[MSFT]

Hi Gary,

yes, for those value types such as DateTime, int, the "Nullable" wrapper in
net 2.0 does be a good way to gracefully handle them like those reference
types(string , class objects...). However, when store into Database, you
still need to use DBNull (database specific null value) depend on the
Nullable instance's value.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi George,

The nullable wrapper does be a new feature of .net framework 2.0. And the
type? syntax is the C# syntax of using nullable type:

#Introduction to .Net Framework 2.0 Nullable Types
http://www.c-sharpcorner.com/UploadFile/mosessaur/nullabletypes0822200616413
5PM/nullabletypes.aspx

#Nullable Generic Structure
http://msdn2.microsoft.com/en-US/library/b3h38hb0(VS.80).aspx


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top