DateTime problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey,

I always struggle with the following:

I have an object obj with a property paydate (type = DateTime)
Now, after instantiating the object and do several things, I want too check if my paydate is still empty.
How can I do this?

I tried several things like (all with errors):
if (obj.paydate == null)
if (obj.paydate.gettype() == System.DBnull)
if (obj.paydate == 01/01/0001)
etc.....

Another question :
When I want to instantiate the object and fill it with data from an sql-query (sql-database). The value received from sql for paydate = null.
How can I test in the datareader that the received data from sql is null and so I put nothing in obj.paydate.


tkx,
Nic
 
nic said:
I always struggle with the following:

I have an object obj with a property paydate (type = DateTime)
Now, after instantiating the object and do several things, I want too
check if my paydate is still empty.

It's not empty to start with. There's no equivalent for null for value
types. Some people choose to use DateTime.MinValue as a "special" value
equivalent to null for DateTimes. Others use a separate property to
indicate whether or not the DateTime is a "real" one.
Another question :
When I want to instantiate the object and fill it with data from an
sql-query (sql-database). The value received from sql for paydate =
null.
How can I test in the datareader that the received data from sql is
null and so I put nothing in obj.paydate.

Use the IsDbNull() in the DataReader. You'll still have to work out
what to put into paydate in that case though.
 
Hi,

If you initalize the paydate as PayDate = new DateTime() in the class
constructor, you can compare paydate with DateTime.MinValue to check if it
still has the initial value. The only problem with this is that if the user
sets paydate to 01/01/0001 00:00:00, then you wont be able to detect the
changes as DateTime.MinValue is nothing but 01/01/0001 00:00:00.

Alternatively, you can maintain your own dirty flag in the set accessor
method of the paydate and check that flag to detect any changes.

For checking null, use SqlDataReader.IsDBNull()

HTH.

hey,

I always struggle with the following:

I have an object obj with a property paydate (type = DateTime)
Now, after instantiating the object and do several things, I want too check
if my paydate is still empty.
How can I do this?

I tried several things like (all with errors):
if (obj.paydate == null)
if (obj.paydate.gettype() == System.DBnull)
if (obj.paydate == 01/01/0001)
etc.....

Another question :
When I want to instantiate the object and fill it with data from an
sql-query (sql-database). The value received from sql for paydate = null.
How can I test in the datareader that the received data from sql is null and
so I put nothing in obj.paydate.


tkx,
Nic
 

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

Similar Threads

DBNULL to DATETIME 4
SqlDateTime and DateTime 2
Passing DateTime 5
SQL: Summing By Date Continuously? 2
DateTime 5
Check if DateTime is valid 1
System.Data.Common.DbDataAdapter Bug. 1
Form requires parameter 7

Back
Top