Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp.net C# dislplay date field from access database

Status
Not open for further replies.

Kurt111780

Technical User
Joined
Nov 20, 2003
Messages
235
Location
GB
Hello,

This should be easy but I can't find the answer anywhere.

Here is my code:

txtAddedDateTime.Text = (string) objRdr["addedDateTime"];

This does not work because the data is stored in an access database as date time data type. I keep getting this error on that line:

System.InvalidCastException: Specified cast is not valid.

How do I modify my code for a date field in access?

Thanks,
Kurt
 
objRdr.GetDateTime(objRdr.GetOrdinal("addedDateTime")).ToString();

you can use any date formatting if you include it in the ToString() function

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks it works great. Now I have another problem. I have another field that is sometimes empty.

If the date field is empty I get this error again:
System.InvalidCastException: Specified cast is not valid.

I changed my code for that field to look like this:

if (objRdr["completedDateTime"] != null)
{
txtCompletedDateTime.Text = (string) objRdr.GetDateTime(objRdr.GetOrdinal("completedDateTime")).ToString();
}


I still get the same error. Why is it still processing that line if the completedDateTime field is null? is there someting wrong with my if statement?

Thanks,
Kurt
 
It seems an access database date field is not null. How do I check to see if the field contains an actual date if its not null?

any suggestions?

Thanks,
Kurt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top