473,385 Members | 1,888 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,385 software developers and data experts.

DBNULL to DATETIME

Hello.
how can i cast a null value from a datareader (System.DBNull) to dateTime

I have an object. One of its fields is a DateTime type field. From the
database i am getting System.DBNull values from its corresponding column.
What can I do to cast it? i don't want to implement a custom method for
this.

myClass.ExpirationDate=Convert.ToDateTime(dr["ExpirationDate"]); // throw an
exception.
Object cannot be cast from DBNULL to other types.

Oct 18 '06 #1
4 35174
Jose Fernandez wrote:
Hello.
how can i cast a null value from a datareader (System.DBNull) to dateTime

I have an object. One of its fields is a DateTime type field. From the
database i am getting System.DBNull values from its corresponding column.
What can I do to cast it? i don't want to implement a custom method for
this.

myClass.ExpirationDate=Convert.ToDateTime(dr["ExpirationDate"]); // throw
an exception.
Object cannot be cast from DBNULL to other types.
You can't. You'll need to come up with some way to represent in your object
the fact that you received a null value from the database. If you are using
..NET 2.0 one option is to make your DateTime in your object a nullable type.
Do this by putting a question mark after the type in the declarations. Ex:

DateTime? date;

This makes the type nullable. Then instead of doing a Convert.ToDateTime
you would need to do a little logic.

if (Convert.IsDBNull(dr["ExpirationDate"]))
myClass.ExpirationDate = null;
else
myClass.ExpirationDate = Convert.ToDateTime(dr["ExpirationDate"]);

Then when reading you would want to check to make sure it has a value before
reading the value.

if (myClass.ExpirationDate.HasValue)
// work with the value
else
// it's null
--
Tom Porterfield

Oct 18 '06 #2
Great, thank you. i could even improve it a little bit by

myClass.ExpirationDate=Convert.IsDBNull(dr["ExpirationDate"])?null :
Convert.ToDateTime(dr["ExpirationDate"])

what do you think? ;)
"Tom Porterfield" <tp******@mvps.orgwrote in message
news:uU**************@TK2MSFTNGP02.phx.gbl...
Jose Fernandez wrote:
>Hello.
how can i cast a null value from a datareader (System.DBNull) to dateTime

I have an object. One of its fields is a DateTime type field. From the
database i am getting System.DBNull values from its corresponding column.
What can I do to cast it? i don't want to implement a custom method for
this.

myClass.ExpirationDate=Convert.ToDateTime(dr["ExpirationDate"]); // throw
an exception.
Object cannot be cast from DBNULL to other types.

You can't. You'll need to come up with some way to represent in your
object the fact that you received a null value from the database. If you
are using .NET 2.0 one option is to make your DateTime in your object a
nullable type. Do this by putting a question mark after the type in the
declarations. Ex:

DateTime? date;

This makes the type nullable. Then instead of doing a Convert.ToDateTime
you would need to do a little logic.

if (Convert.IsDBNull(dr["ExpirationDate"]))
myClass.ExpirationDate = null;
else
myClass.ExpirationDate = Convert.ToDateTime(dr["ExpirationDate"]);

Then when reading you would want to check to make sure it has a value
before reading the value.

if (myClass.ExpirationDate.HasValue)
// work with the value
else
// it's null
--
Tom Porterfield

Oct 18 '06 #3
Jose Fernandez wrote:
Great, thank you. i could even improve it a little bit by

myClass.ExpirationDate=Convert.IsDBNull(dr["ExpirationDate"])?null :
Convert.ToDateTime(dr["ExpirationDate"])

what do you think? ;)
Now your getting down to coding styles. Whether or not that is an
improvement is a matter of taste.
--
Tom Porterfield

Oct 18 '06 #4
Now your getting down to coding styles. Whether or not that is an
improvement is a matter of taste.
Agreed. <shameless_plug>For that very reason we created "Compress to Ternary
Expression" and "Expand Ternary Expression" refactorings in our Refactor!
Pro product.</shameless_plug>

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #5

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

Similar topics

9
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
6
by: Paulb1us | last post by:
I want to set a DateTime field to Null before passing it to the DB //First I check to see if anything is in this datarow column, because sometimes we have no data. DateTime dt; if (...
2
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
4
by: Dursun | last post by:
Hi, I am trying to assign NULL to a datetime field in the SQL Server database. Here is the code that does NOT work: INSERT INTO ... .... VALUES ... .... CType(IIf(dateWitness2Date.Checked,...
2
by: jonefer | last post by:
I have effectively managed to get SQL server to stop complaining when I provide the DBNULL.Value to my parameter, but it doesn't replace (I would like it to ) the date currently in that field with...
4
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember =...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
0
by: paulnamroud | last post by:
Hello, I'm trying to send a Null value in a DateTime field while calling my stored procedure. If I use this first method (short with one line), i got the following error message: ...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.