473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dealing with null and datetime

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.

--
Regards,
Gary Blakely
Mar 20 '07 #1
12 2449
"GaryDean" <Ga******@newsgroups.nospamwrote in message
news:u7**************@TK2MSFTNGP05.phx.gbl...
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;
Mar 20 '07 #2
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.
"GaryDean" <Ga******@newsgroups.nospamwrote in message
news:u7**************@TK2MSFTNGP05.phx.gbl...
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.

--
Regards,
Gary Blakely


Mar 20 '07 #3
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.
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:ON**************@TK2MSFTNGP02.phx.gbl...
"GaryDean" <Ga******@newsgroups.nospamwrote in message
news:u7**************@TK2MSFTNGP05.phx.gbl...
>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;

Mar 20 '07 #4
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:O7**************@TK2MSFTNGP03.phx.gbl...
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.
Mar 20 '07 #5
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
The problem is that DateTime is a value type.
That's right.
Value type can not be null.
That's also right.

However, DateTime? can be null...

Mar 20 '07 #6
GaryDean wrote:
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öran Andersson
_____
http://www.guffa.com
Mar 20 '07 #7
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.


"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:u6**************@TK2MSFTNGP06.phx.gbl...
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:O7**************@TK2MSFTNGP03.phx.gbl...
>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.

Mar 20 '07 #8
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:uH**************@TK2MSFTNGP04.phx.gbl...
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.
Mar 20 '07 #9
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));
}

--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:ON**************@TK2MSFTNGP02.phx.gbl...
"GaryDean" <Ga******@newsgroups.nospamwrote in message
news:u7**************@TK2MSFTNGP05.phx.gbl...
>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;

Mar 21 '07 #10
"GaryDean" <Ga******@newsgroups.nospamwrote in message
news:ui**************@TK2MSFTNGP04.phx.gbl...
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.
Mar 21 '07 #11
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.

Mar 21 '07 #12
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/UploadF...s0822200616413
5PM/nullabletypes.aspx

#Nullable Generic Structure
http://msdn2.microsoft.com/en-US/lib...b0(VS.80).aspx
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 21 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sean Newton | last post by:
I am absolutely bewildered by now by the Microsoft.Samples SSPI and Security assemblies. I've been trying to set these up in a very straightforward harness in the way that I'd like to be able to...
9
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
2
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime...
8
by: Manish Jain | last post by:
Platform : ASP.Net/C# void SomeFunction(DateTime date) { string text = date.ToString(); //This crashes if date is null } I am using a construct similar to above. I want to check if the date...
1
by: Dean L. Howen | last post by:
Dear friends, Could you help me to solve the problem: How can we set/get NULL values of datetime in DatetimePicker ? I design a form using DateTimePicker to input/output date value, but the date...
5
by: Rene | last post by:
I have my object model thing going with some classes that contain properties that are saved into a database. Some of these properties are *numeric* properties that can be left Null in the database...
5
by: tshad | last post by:
I have the following code: *************************************************************************** Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position (client,dateposted) VALUES...
3
by: Daves | last post by:
a get { ... } for public property SelectedValue returns DateTime type to be used as a parameter in a Sql update query but I'd like it to return "empty" if no date has been selected... I cannot use...
7
by: Sala | last post by:
Hi experts! If system time 12:00 am my application variable value will be null.. I am trying to get null value of application but that time i'll make some actions to that page.. So pls tell me how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.