Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Formatting Date for DataList Field
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Formatting Date for DataList Field

 Post Reply Post Reply
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Formatting Date for DataList Field
    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!
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2005 at 2:12am
Do you mean

<%# CDate(DataBinder.Eval(Container.DataItem, "DateSigned")) %>?
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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)%>
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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")) %>
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2005 at 2:18pm
use

If Not TypeOf d Is DBNull Then

...
End If
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2005 at 2:57pm
The line of code that Michael gave solved the problem. Thanks for your help!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.