Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Date format in repeater control

Reply
Thread Tools

Date format in repeater control

 
 
zdrakec
Guest
Posts: n/a
 
      07-12-2005
Hello all:

I'm having no problem with formatting a date, when I have one, but when
the Container.DataItem is DBNull, then my date formatting blows up
(Cast from DBNull to Date not valid). I've tried numerous ways of
testing for DBNull before doing the format, but have had no luck...

Any ideas?

Thanks much,

zdrakec

 
Reply With Quote
 
 
 
 
zdrakec
Guest
Posts: n/a
 
      07-12-2005
Never mind, got it:

DataBinder.Eval(Container.DataItem,"DateField","{0 :MM/dd/yyyy}")

Simple really...

Thanks anyway!!

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-12-2005
Hi,

if you don't need to cast to a date first, you could simply have a
databinding expression in the aspx. For example:

<asp:TemplateColumn>
<ItemTemplate>
<%#
DataBinder.Eval(Container.DataItem,"database_field _name","{0:dd.MM.yyyy}")%>
</ItemTemplate>
</asp:TemplateColumn>

where "{0:dd.MM.yyyy}" indicates the formatting. Now, databinding
expressions should not blow up with null values (DBNull), they just produce
empty string.

However, if you need to cast to a date in code, you can nothing but check
for the DbNull value from the field with IsDbNull function (VB) or
Convert.IsDbNull (Framework/C#) which return boolean value indicating if the
value is DbNull. When there is a DbNull (per previous methods) you just
outout empty string (or other predefined string such as -No date-), you
cannot cast it to a Date. So it means that this check should happen before
trying casting.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke






 
Reply With Quote
 
zdrakec
Guest
Posts: n/a
 
      07-12-2005
Right, that is what I was doing, only I wasn't using the Eval... in
other words, I was trying to do:

IIF(IsDBNull(datavalue),"",Format(datavalue,"MM/dd/yyyy"))

which was failing because the IIF does not behave like
If..AndAlso...End If nor like If...OrElse...End If, and I was treating
it as though it did; so the "false" condition of the IIF was getting
examined, and blowing up for DBNull values.

I changed it to

IIF(IsDBNull(Container.DataItem("DateField")),"",D ataBinder.Eval(Container.DataItem,"DateField","{0: MM/dd/yyyy}"))

which works just fine since the DBNull evaluates to an empty string, as
you said.

Thanks!
zdrakec

 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-12-2005
It is better to use If Else explicitly with these, or self-made helper
library functions. As you say, IIF always executes both functions despite
the outcome of the first expression which defines what is returned.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeater control with nested repeater chris.reed@digus.com ASP .Net 0 02-11-2005 05:10 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments