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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

format a date in a repeater

Status
Not open for further replies.

treyball3

Programmer
Jun 20, 2001
74
US
Ok, so I have a repeater on my aspx page. One of the fields is a date. I want to format this to the shortdate format. The date comes from a database, so it could be null, which is what causes the problem. Here is my code...

Code:
<%# iif(not (DataBinder.Eval(Container.DataItem, "DueDate") is System.DBNull.Value), FormatDateTime(DataBinder.Eval(Container.DataItem, "DueDate"), DateFormat.ShortDate), "") %>

I get an error...

"System.InvalidCastException: Cast from type 'DBNull' to type 'Date' is not valid"

I think I know WHY I get this error... its like the second Container.DataItem takes precendence over the iif. Not sure why... and I don't know how to fix it!


But if I simply do

Code:
<%# iif(not (DataBinder.Eval(Container.DataItem, "DueDate") is System.DBNull.Value), "1", "2") %>

then I get the results I expect. So, I know the check is working correctly.

Does anyone know how to fix this?

Thanks!



Thanks - Todd
 
I wouldn't go through all this to format a date.
Instead use a DataBound column use set EncodeHTML=false, set the FormatString="{0:MM-dd-yy}" and set the EmptyField="" (or what ever the empty string property is named.)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top