IIF statement used to evaluate date and covert it to shortdate driving me to distraction

Discussion in 'ASP .Net' started by Jim Florence, Jun 24, 2006.

  1. Jim Florence

    Jim Florence Guest

    Hello,

    I've just started in ASP and I'm having a few teething problems. Initially I
    tried to write out dates from the database using

    <asp:Label runat="server" ID="Label6" Text='<%# Eval("ShippedDate") %>

    But I got a problem with DBNull's, a kind sould told me to look at using IIF
    and that sorted part of the problem. It bypassed the Nulls but didn't
    actually put the text in that I'd put in the statement.This is the code

    asp:Label runat="server" ID="Label10" Text='<%# IIF (Eval("ShippedDate")is
    nothing,"My Text",Container.DataItem("ShippedDate")) %>' />

    After more digging I changed the code to the floowing to check for dbnull
    and the text finally appeared on the page.

    <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    Eval("ShippedDate") ) is DbNull ,"No Date",Eval("ShippedDate")) %>' />

    The final piece I was trying to do was convert the date to a shortdate and
    used the folowing code.

    <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    Eval("ShippedDate") ) is DbNull ,"No
    Date",FormatDateTime(Eval("ShippedDate"),vbShortDate)) %>' />

    This now again gives me a "Conversion from type 'DBNull' to type 'Date' is
    not valid." when I run the code.

    I'm now officially confused, any help gratefully recieved

    Regards

    Jim Florence
     
    Jim Florence, Jun 24, 2006
    #1
    1. Advertising

  2. Hi Jim,

    In this case, I'd use a helper function to get around the complicated IIF
    stuff.

    The problem is that you've got to be ready for a DBNull at any time, so it's
    easier to look at it as an Object.

    Here's a little helper function that might do what you need. The complete
    source is below.

    Function fixnull(ByVal datetm As Object) _
    As String
    If IsDBNull(datetm) Then
    Return "No Date"
    End If
    Return FormatDateTime(datetm, vbShortDate)
    End Function

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]


    <%@ page language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub Page_Load _
    (ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If Not IsPostBack Then
    Repeater1.DataSource = CreateDataSource()
    Repeater1.DataBind()
    End If
    End Sub

    Function fixnull(ByVal datetm As Object) _
    As String
    If IsDBNull(datetm) Then
    Return "No Date"
    End If
    Return FormatDateTime(datetm, vbShortDate)
    End Function


    Function CreateDataSource() As Data.DataTable
    Dim dt As New Data.DataTable
    Dim dr As Data.DataRow
    dt.Columns.Add(New Data.DataColumn _
    ("ShippedDate", GetType(DateTime)))
    dt.Columns.Add(New Data.DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New Data.DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New Data.DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 5
    dr = dt.NewRow()
    If i = 3 Then
    dr(0) = System.DBNull.Value
    Else
    dr(0) = Now.AddDays(i)
    End If

    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Fix a DBNull problem</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:repeater id="Repeater1" runat="server">
    <itemtemplate ><p>
    <asp:label id="Label6" runat="server" text='<%#
    fixnull(Eval("ShippedDate")) %>'></asp:label></p>
    </itemtemplate>
    </asp:repeater>
    </div>
    </form>
    </body>
    </html>



    "Jim Florence" <> wrote in message
    news:...
    >
    > Hello,
    >
    > I've just started in ASP and I'm having a few teething problems. Initially
    > I tried to write out dates from the database using
    >
    > <asp:Label runat="server" ID="Label6" Text='<%# Eval("ShippedDate") %>
    >
    > But I got a problem with DBNull's, a kind sould told me to look at using
    > IIF and that sorted part of the problem. It bypassed the Nulls but didn't
    > actually put the text in that I'd put in the statement.This is the code
    >
    > asp:Label runat="server" ID="Label10" Text='<%# IIF (Eval("ShippedDate")is
    > nothing,"My Text",Container.DataItem("ShippedDate")) %>' />
    >
    > After more digging I changed the code to the floowing to check for dbnull
    > and the text finally appeared on the page.
    >
    > <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    > Eval("ShippedDate") ) is DbNull ,"No Date",Eval("ShippedDate")) %>' />
    >
    > The final piece I was trying to do was convert the date to a shortdate and
    > used the folowing code.
    >
    > <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    > Eval("ShippedDate") ) is DbNull ,"No
    > Date",FormatDateTime(Eval("ShippedDate"),vbShortDate)) %>' />
    >
    > This now again gives me a "Conversion from type 'DBNull' to type 'Date' is
    > not valid." when I run the code.
    >
    > I'm now officially confused, any help gratefully recieved
    >
    > Regards
    >
    > Jim Florence
     
    Ken Cox [Microsoft MVP], Jun 24, 2006
    #2
    1. Advertising

  3. Jim Florence

    Jim Florence Guest

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:e56jvD%...
    Ken, Shawn,

    Thanks very much for your quick replies they were both extermely helpful.

    Shawn, I'll have a deeper look at this as it seems very useful and a great
    way to do it.I've only been looking at ASP for a couple of days so It's a
    steep learning curve!

    Ken, that was so simple and worked straight away, that method is nice and
    simple and will also work so much better for another couple of things I've
    tried to do and ended up making over complex

    Thank you both for helping me not throw my laptop through the window!!! :)

    Regards

    Jim

    > Hi Jim,
    >
    > In this case, I'd use a helper function to get around the complicated IIF
    > stuff.
    >
    > The problem is that you've got to be ready for a DBNull at any time, so
    > it's easier to look at it as an Object.
    >
    > Here's a little helper function that might do what you need. The complete
    > source is below.
    >
    > Function fixnull(ByVal datetm As Object) _
    > As String
    > If IsDBNull(datetm) Then
    > Return "No Date"
    > End If
    > Return FormatDateTime(datetm, vbShortDate)
    > End Function
    >
    > Let us know if this helps?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    >
    > <%@ page language="VB" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <script runat="server">
    >
    > Protected Sub Page_Load _
    > (ByVal sender As Object, _
    > ByVal e As System.EventArgs)
    > If Not IsPostBack Then
    > Repeater1.DataSource = CreateDataSource()
    > Repeater1.DataBind()
    > End If
    > End Sub
    >
    > Function fixnull(ByVal datetm As Object) _
    > As String
    > If IsDBNull(datetm) Then
    > Return "No Date"
    > End If
    > Return FormatDateTime(datetm, vbShortDate)
    > End Function
    >
    >
    > Function CreateDataSource() As Data.DataTable
    > Dim dt As New Data.DataTable
    > Dim dr As Data.DataRow
    > dt.Columns.Add(New Data.DataColumn _
    > ("ShippedDate", GetType(DateTime)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("StringValue", GetType(String)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("CurrencyValue", GetType(Double)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("Boolean", GetType(Boolean)))
    > Dim i As Integer
    > For i = 0 To 5
    > dr = dt.NewRow()
    > If i = 3 Then
    > dr(0) = System.DBNull.Value
    > Else
    > dr(0) = Now.AddDays(i)
    > End If
    >
    > dr(1) = "Item " + i.ToString()
    > dr(2) = 1.23 * (i + 1)
    > dr(3) = (i = 4)
    > dt.Rows.Add(dr)
    > Next i
    > Return dt
    > End Function
    > </script>
    >
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head runat="server">
    > <title>Fix a DBNull problem</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:repeater id="Repeater1" runat="server">
    > <itemtemplate ><p>
    > <asp:label id="Label6" runat="server" text='<%#
    > fixnull(Eval("ShippedDate")) %>'></asp:label></p>
    > </itemtemplate>
    > </asp:repeater>
    > </div>
    > </form>
    > </body>
    > </html>
    >
    >
    >
    > "Jim Florence" <> wrote in message
    > news:...
    >>
    >> Hello,
    >>
    >> I've just started in ASP and I'm having a few teething problems.
    >> Initially I tried to write out dates from the database using
    >>
    >> <asp:Label runat="server" ID="Label6" Text='<%# Eval("ShippedDate") %>
    >>
    >> But I got a problem with DBNull's, a kind sould told me to look at using
    >> IIF and that sorted part of the problem. It bypassed the Nulls but didn't
    >> actually put the text in that I'd put in the statement.This is the code
    >>
    >> asp:Label runat="server" ID="Label10" Text='<%# IIF
    >> (Eval("ShippedDate")is nothing,"My
    >> Text",Container.DataItem("ShippedDate")) %>' />
    >>
    >> After more digging I changed the code to the floowing to check for dbnull
    >> and the text finally appeared on the page.
    >>
    >> <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    >> Eval("ShippedDate") ) is DbNull ,"No Date",Eval("ShippedDate")) %>' />
    >>
    >> The final piece I was trying to do was convert the date to a shortdate
    >> and used the folowing code.
    >>
    >> <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    >> Eval("ShippedDate") ) is DbNull ,"No
    >> Date",FormatDateTime(Eval("ShippedDate"),vbShortDate)) %>' />
    >>
    >> This now again gives me a "Conversion from type 'DBNull' to type 'Date'
    >> is not valid." when I run the code.
    >>
    >> I'm now officially confused, any help gratefully recieved
    >>
    >> Regards
    >>
    >> Jim Florence

    >
    >
     
    Jim Florence, Jun 25, 2006
    #3
  4. > Thank you both for helping me not throw my laptop through the window!!! :)

    Glad to help! And let me know where to stand on the sidewalk when you decide
    to toss that laptop? <grin>

    Ken
    Microsoft MVP [ASP.NET]


    "Jim Florence" <> wrote in message
    news:...
    >
    > "Ken Cox [Microsoft MVP]" <> wrote in
    > message news:e56jvD%...
    > Ken, Shawn,
    >
    > Thanks very much for your quick replies they were both extermely helpful.
    >
    > Shawn, I'll have a deeper look at this as it seems very useful and a great
    > way to do it.I've only been looking at ASP for a couple of days so It's a
    > steep learning curve!
    >
    > Ken, that was so simple and worked straight away, that method is nice and
    > simple and will also work so much better for another couple of things I've
    > tried to do and ended up making over complex
    >
    > Thank you both for helping me not throw my laptop through the window!!! :)
    >
    > Regards
    >
    > Jim
    >
    >> Hi Jim,
    >>
    >> In this case, I'd use a helper function to get around the complicated IIF
    >> stuff.
    >>
    >> The problem is that you've got to be ready for a DBNull at any time, so
    >> it's easier to look at it as an Object.
    >>
    >> Here's a little helper function that might do what you need. The complete
    >> source is below.
    >>
    >> Function fixnull(ByVal datetm As Object) _
    >> As String
    >> If IsDBNull(datetm) Then
    >> Return "No Date"
    >> End If
    >> Return FormatDateTime(datetm, vbShortDate)
    >> End Function
    >>
    >> Let us know if this helps?
    >>
    >> Ken
    >> Microsoft MVP [ASP.NET]
    >>
    >>
    >> <%@ page language="VB" %>
    >>
    >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>
    >> <script runat="server">
    >>
    >> Protected Sub Page_Load _
    >> (ByVal sender As Object, _
    >> ByVal e As System.EventArgs)
    >> If Not IsPostBack Then
    >> Repeater1.DataSource = CreateDataSource()
    >> Repeater1.DataBind()
    >> End If
    >> End Sub
    >>
    >> Function fixnull(ByVal datetm As Object) _
    >> As String
    >> If IsDBNull(datetm) Then
    >> Return "No Date"
    >> End If
    >> Return FormatDateTime(datetm, vbShortDate)
    >> End Function
    >>
    >>
    >> Function CreateDataSource() As Data.DataTable
    >> Dim dt As New Data.DataTable
    >> Dim dr As Data.DataRow
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("ShippedDate", GetType(DateTime)))
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("StringValue", GetType(String)))
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("CurrencyValue", GetType(Double)))
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("Boolean", GetType(Boolean)))
    >> Dim i As Integer
    >> For i = 0 To 5
    >> dr = dt.NewRow()
    >> If i = 3 Then
    >> dr(0) = System.DBNull.Value
    >> Else
    >> dr(0) = Now.AddDays(i)
    >> End If
    >>
    >> dr(1) = "Item " + i.ToString()
    >> dr(2) = 1.23 * (i + 1)
    >> dr(3) = (i = 4)
    >> dt.Rows.Add(dr)
    >> Next i
    >> Return dt
    >> End Function
    >> </script>
    >>
    >> <html xmlns="http://www.w3.org/1999/xhtml">
    >> <head runat="server">
    >> <title>Fix a DBNull problem</title>
    >> </head>
    >> <body>
    >> <form id="form1" runat="server">
    >> <div>
    >> <asp:repeater id="Repeater1" runat="server">
    >> <itemtemplate ><p>
    >> <asp:label id="Label6" runat="server" text='<%#
    >> fixnull(Eval("ShippedDate")) %>'></asp:label></p>
    >> </itemtemplate>
    >> </asp:repeater>
    >> </div>
    >> </form>
    >> </body>
    >> </html>
    >>
    >>
    >>
    >> "Jim Florence" <> wrote in message
    >> news:...
    >>>
    >>> Hello,
    >>>
    >>> I've just started in ASP and I'm having a few teething problems.
    >>> Initially I tried to write out dates from the database using
    >>>
    >>> <asp:Label runat="server" ID="Label6" Text='<%# Eval("ShippedDate") %>
    >>>
    >>> But I got a problem with DBNull's, a kind sould told me to look at using
    >>> IIF and that sorted part of the problem. It bypassed the Nulls but
    >>> didn't actually put the text in that I'd put in the statement.This is
    >>> the code
    >>>
    >>> asp:Label runat="server" ID="Label10" Text='<%# IIF
    >>> (Eval("ShippedDate")is nothing,"My
    >>> Text",Container.DataItem("ShippedDate")) %>' />
    >>>
    >>> After more digging I changed the code to the floowing to check for
    >>> dbnull and the text finally appeared on the page.
    >>>
    >>> <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    >>> Eval("ShippedDate") ) is DbNull ,"No Date",Eval("ShippedDate")) %>' />
    >>>
    >>> The final piece I was trying to do was convert the date to a shortdate
    >>> and used the folowing code.
    >>>
    >>> <asp:Label runat="server" ID="Label6" Text='<%# IIF (typeof (
    >>> Eval("ShippedDate") ) is DbNull ,"No
    >>> Date",FormatDateTime(Eval("ShippedDate"),vbShortDate)) %>' />
    >>>
    >>> This now again gives me a "Conversion from type 'DBNull' to type 'Date'
    >>> is not valid." when I run the code.
    >>>
    >>> I'm now officially confused, any help gratefully recieved
    >>>
    >>> Regards
    >>>
    >>> Jim Florence

    >>
    >>
     
    Ken Cox [Microsoft MVP], Jun 25, 2006
    #4
    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. Max
    Replies:
    2
    Views:
    4,659
  2. Summu82
    Replies:
    5
    Views:
    872
    Richard Heathfield
    Jun 7, 2006
  3. jonny
    Replies:
    1
    Views:
    444
    David R. Longnecker
    Oct 10, 2007
  4. George

    IIF and FieldCount Property of Container.DataIte,

    George, Jun 8, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    128
    [MSFT]
    Jun 8, 2004
  5. irene

    How to display shortdate format on a datagrid?

    irene, Oct 27, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    364
    Scott Mitchell [MVP]
    Oct 27, 2004
Loading...

Share This Page