473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Date format in repeater control

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

Nov 19 '05 #1
4 7294
Never mind, got it:

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

Simple really...

Thanks anyway!!

Nov 19 '05 #2
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


Nov 19 '05 #3
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

Nov 19 '05 #4
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
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James Radke | last post by:
Hello! I have a webpage (vb.net developed in Visual Studio.NET 2003) which has a repeater control which has as one of its columns an editable textbox. This textbox allows the user to enter a...
4
by: Darrel | last post by:
I'm stumped on something that would seem to be pretty simple. I have data bound to a repeater control. One of the fields is a date field from the SQL table: ...
0
by: Shapper | last post by:
Hello, I have an ASP.Net Repeater where I display a date from a date field: <%# DataBinder.Eval(Container.DataItem, "pubDate", "{0:f}") %> When the dataset is created from a database the date...
9
by: Shapper | last post by:
Hello, I created a dataset from a XML file. One of the dataset fields is . In an ASP:Repeater I am displaying the field. <%# DataBinder.Eval(Container.DataItem, "pubDate", "{0:f}") %> The...
2
by: Sun | last post by:
Hi, I display date in asp.net pages using user defined format: mydate.text=Format(dr("MyDate"), "MMM. d, yyyy"). It works fine for all pages except one. In that page, I built a dataAdapter,...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
4
by: shapper | last post by:
Hello, I have created a GridView on runtime by implementing the ITemplate class. This Gridview displays a list of articles. Each article has a Title, Content and PubDate. I am trying to...
4
by: =?Utf-8?B?ZGNoMw==?= | last post by:
Is there a way to conditionally format a dateTime field to produce on result if the value is 10/31/2008 12:00 AM (user didn't enter a time) and another result if the value is 10/31/2008 5:30 PM...
1
by: David C | last post by:
I am trying to get a LinkButton to show a formatted date as part of the text but it is not working. Below is what I have. MealDate is a valid date, e.g. 11/17/2008. I want it to come out like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.