Date format in repeater control

Discussion in 'ASP .Net' started by zdrakec, Jul 12, 2005.

  1. zdrakec

    zdrakec Guest

    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
    zdrakec, Jul 12, 2005
    #1
    1. Advertising

  2. zdrakec

    zdrakec Guest

    Never mind, got it:

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

    Simple really...

    Thanks anyway!!
    zdrakec, Jul 12, 2005
    #2
    1. Advertising

  3. zdrakec

    Teemu Keiski Guest

    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
    Teemu Keiski, Jul 12, 2005
    #3
  4. zdrakec

    zdrakec Guest

    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")),"",DataBinder.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
    zdrakec, Jul 12, 2005
    #4
  5. zdrakec

    Teemu Keiski Guest

    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
    Teemu Keiski, Jul 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,159
    Janaka
    Feb 18, 2004
  2. DC
    Replies:
    5
    Views:
    3,005
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,251
    Michael Borgwardt
    May 30, 2004
  4. Chu
    Replies:
    3
    Views:
    4,186
    Shawn
    Aug 15, 2006
  5. Josh Sharpe
    Replies:
    1
    Views:
    206
    Brian Candler
    Sep 21, 2010
Loading...

Share This Page