472,976 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problems with nullable DateTime

Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill

Feb 14 '07 #1
4 10644
Forget it, I figured it out as I sit here hitting my head for being so
stupid.
"Bill Gower" <bi*******@charter.netwrote in message
news:OR**************@TK2MSFTNGP03.phx.gbl...
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field
in the table is not able to be represented as a string. Which is fine, I
can understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill

Feb 14 '07 #2
On Feb 14, 2:09 pm, "Bill Gower" <billgo...@charter.netwrote:
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.

Bill
The above won't work because: oldRow["datemember"].ToString() will
fail if oldRow["datemember"] is null.

Test for if your datetime field equals to DBNull.Value:

if (oldRow["datemember"] != DBNull.Value)
DateMember = DateTime.Parse(oldRow["datemember"].ToString());
else
DateMember = DateTime.MinValue; //You cannot assign a DateTime
value to null

One liner:
DateMember = oldRow["datemember"] == DBNull.Value ?
DateTime.MinValue :
DateTime.Parse(oldRow["datemember"].ToString());

Have fun.

Quoc Linh

Feb 14 '07 #3
Bill Gower <bi*******@charter.netwrote:
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());
Well, that's not your actual code - ToString is a method, not a
property. In future, it would be helpful to post the actual code.
The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
You should examine the value of oldRow["datemember"] *before* trying to
parse it, instead of after.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #4
Mel
to check a field use

oldRow["datemember"] == DBNull.Value

"Bill Gower" <bi*******@charter.netwrote in message
news:OR**************@TK2MSFTNGP03.phx.gbl...
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field
in the table is not able to be represented as a string. Which is fine, I
can understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill

Feb 14 '07 #5

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

Similar topics

4
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...
9
by: Mark Rae | last post by:
Hi, I posted a couple of days ago about the possibility of "simulating" in ..NET1.1 the nullable datatypes available in .NET2.0 - I'm nearly there, but require a bit more guidance. Basically,...
0
by: pranesh.nayak | last post by:
Hello Group, (tech: C#, VS2005) I'm facing a problem in passing Nullable<DateTimeto a webservice. Below is code in webservice wrapper (reference.cs) used set/get date. When i set date value...
5
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...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
5
by: =?Utf-8?B?emlubw==?= | last post by:
in .net 2.0, the class (myClass) contains a property defined as: private _creationDate as As Nullable(Of DateTime) Public Property CreationDate() As Nullable(Of DateTime) Get If...
2
by: Benton | last post by:
Hi there, I'm creating a custom server control, inheriting from TextBox. It has this AsDateTime property that returns the textbox contents converted to the nullable DateTime data type, as...
6
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a generic function I am using to check constraints, an example of which is the following: Public Function ValidateMinValue(Of t As IComparable)(ByVal PropertyName As String, ByVal value...
3
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
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...
2
by: Andrus | last post by:
My type contains constructor with nullable parameter like public class Iandmed { public Iandmed(DateTime? miskuup) {} } I tried to get its constructor parameter type name using Type t =...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.