473,799 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nullable Dates

I have a start and end date in my application. If a user does not know their
dates yet, I want them, they will be null in the DB and I want them to be
blank in the application. So, I'm trying to figure out how to use a nullable
date, and want to return it in my method.

I tried to cast the output parameter, but I get a run-time cast error:
return new TimeLine((DateT ime?)command.Pa rameters["@startDate "].Value,
(DateTime?)comm and.Parameters["@endDate"].Value);
Then I changed to the code to what you see below, and it worked. But I am
wondering is there a better way to do this when working with nullable dates?

DateTime? startDate;
DateTime? endDate;

if (command.Parame ters["@startDate "].Value is DBNull)
startDate = null;
else
startDate = (DateTime?)comm and.Parameters["@startDate "].Value;

if (command.Parame ters["@endDate"].Value is DBNull)
endDate = null;
else
endDate = (DateTime?)comm and.Parameters["@endDate"].Value;

return new TimeLine(startD ate, endDate);

Sep 13 '07 #1
3 1945
On Sep 13, 3:12 pm, Wannabe <Wann...@discus sions.microsoft .comwrote:
I have a start and end date in my application. If a user does not know their
dates yet, I want them, they will be null in the DB and I want them to be
blank in the application. So, I'm trying to figure out how to use a nullable
date, and want to return it in my method.

I tried to cast the output parameter, but I get a run-time cast error:
return new TimeLine((DateT ime?)command.Pa rameters["@startDate "].Value,
(DateTime?)comm and.Parameters["@endDate"].Value);

Then I changed to the code to what you see below, and it worked. But I am
wondering is there a better way to do this when working with nullable dates?
The simplest way would be:

DateTime? startDate = command.Paramet ers["@startDate "].Value as
DateTime? ;
DateTime? endDate = command.Paramet ers["@endDate"].Value as
DateTime? ;

That will work, *but* it will give you null for start/end dates which
are neither DBNull nor DateTime. In other words, it will hide the
error. If you want more rigour, I'd write a helper method to convert
object to DateTime, throwing an exception if the parameter is neither
a DateTime nor DBNull.Value.

Jon

Sep 13 '07 #2
Hi,

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ r34g2000hsd.goo glegroups.com.. .
On Sep 13, 3:12 pm, Wannabe <Wann...@discus sions.microsoft .comwrote:
>I have a start and end date in my application. If a user does not know
their
dates yet, I want them, they will be null in the DB and I want them to be
blank in the application. So, I'm trying to figure out how to use a
nullable
date, and want to return it in my method.

I tried to cast the output parameter, but I get a run-time cast error:
return new TimeLine((DateT ime?)command.Pa rameters["@startDate "].Value,
(DateTime?)com mand.Parameters["@endDate"].Value);

Then I changed to the code to what you see below, and it worked. But I am
wondering is there a better way to do this when working with nullable
dates?

The simplest way would be:

DateTime? startDate = command.Paramet ers["@startDate "].Value as
DateTime? ;
DateTime? endDate = command.Paramet ers["@endDate"].Value as
DateTime? ;

That will work, *but* it will give you null for start/end dates which
are neither DBNull nor DateTime. In other words, it will hide the
error. If you want more rigour, I'd write a helper method to convert
object to DateTime, throwing an exception if the parameter is neither
a DateTime nor DBNull.Value.
If the columns in the DB are of DateTime then it will work as expected.
Sep 13 '07 #3
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
That will work, *but* it will give you null for start/end dates
which
are neither DBNull nor DateTime. In other words, it will hide the
error. If you want more rigour, I'd write a helper method to convert
object to DateTime, throwing an exception if the parameter is neither
a DateTime nor DBNull.Value.

If the columns in the DB are of DateTime then it will work as expected.
Indeed. I was just warning that if the columns were changed
inappropriately , it would fail silently (by returning null) rather than
screaming from the rooftops about the type being wrong :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 16 '07 #4

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

Similar topics

4
5972
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what specific type the nullable type is.) The reason I'm trying this is because when I attempt to pass a nullable type value to a SqlCommand parameter and then attempt to execute it I the following error: "No mapping exists from object type...
0
1370
by: tg.foobar | last post by:
i'm using a propertygrid to show / edit values of a class that i have. I'm using the new(er) nullable types and it's showing the value of these items as empty (as i would think), but the propertygrid doesn't seem to associate the proper editing control. for example: i have: DateTime? _actualReceivedDate; .....
8
4856
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
0
1409
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously crippled), I don't know. But it seems to me obvious that there should be some way to specify that DB fields which can be null should be mapped to appropriate Nullable(Of T) generic types. For fields there's a dropdown in the dataset designer...
1
5681
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide) http://msdn2.microsoft.com/en-us/library/ms366789.aspx however, using their code snippet, I couldn't get it to work:
5
51682
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any ideas?
8
10704
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
2
1797
Cathode Follower
by: Cathode Follower | last post by:
In VB6 we put date values obtained from databases into variants. We can then use statements like IsNull(.EndDate) to check whether a date is set or not. When you put code like this through the vb.net conversion wizard it turns out very messy and you have to make lots of changes by hand. The basic principle is that dates like this have to be represented as Nullable(Of Date) in vb.net. In order to make life simpler, I have introduced two simple...
2
180
by: Tony Johansson | last post by:
Hello! These two declarations(1 and 2) are the same I assume. 1. System.Nullable<intnullable; 2. System.Nullable<intnullable = new System.Nullable<int(); So because these 1 and 2 are the same is no point to use the longer declaration as 2 it good enough to use decaration 1.
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.