Print Page | Close Window

Formatting Date for DataList Field

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=13485
Printed Date: 29 March 2026 at 12:33pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Formatting Date for DataList Field
Posted By: Misty
Subject: Formatting Date for DataList Field
Date Posted: 24 January 2005 at 12:59am
I need help with formatting the date for a date field in a datalist that I have in ASP.Net.
 
Here's the line of code: <%# DataBinder.Eval(Container.DataItem, "DateSigned") %>
 
I know that you would do something like Dim CurrentDate as DateTime = DateTime.Now and then something like: litDate.Text = CurrentDate.Date
 
But I am not sure how you would format the DateSigned field in the line of code above. Can someone please help me? Thanks!



Replies:
Posted By: Mart
Date Posted: 24 January 2005 at 2:12am
Do you mean

<%# CDate(DataBinder.Eval(Container.DataItem, "DateSigned")) %>?


Posted By: Misty
Date Posted: 24 January 2005 at 2:25am
I got the following error message: Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'Date' is not valid.
 
In ASP, I used <%= FormatDateTime(DateSigned, 2)%>. It worked fine. It gave me the date I want (ex: 1/24/2005). This is what I would like to happen with the DateSigned field in my ASP.Net page. <%= atDateTime(DateSigned, 2)%><%= atDateTime(DateSigned, 2)%><%= atDateTime(DateSigned, 2)%>


Posted By: Mart
Date Posted: 24 January 2005 at 11:12am
The reason you get that error is because some of the fields in your table are null... you can use some code like

Public Function FDate(d As Object) As String
If Not d Is Nothing Then
Return CDate(d).ToShortDateString()
End If
End Function

Then

<%# FDate(DataBinder.Eval(Container.DataItem, "DateSigned")) %>


Posted By: Misty
Date Posted: 24 January 2005 at 1:18pm
I used the above code that you gave me. But it is still not working. I got the following error message: Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'Date' is not valid.
 
Line 13:         Public Function FDate(d As Object) As String
Line 14:         If Not d Is Nothing Then
Line 15:         Return CDate(d).ToShortDateString()
Line 16:         End If
Line 17:         End Function
 
I wonder why this won't work. It's easy to format a date field in ASP.


Posted By: michael
Date Posted: 24 January 2005 at 2:18pm
use

If Not TypeOf d Is DBNull Then

...
End If


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date Posted: 24 January 2005 at 2:57pm
The line of code that Michael gave solved the problem. Thanks for your help!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net