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

J

Jim Florence

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
 
K

Ken Cox [Microsoft MVP]

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>
 
J

Jim Florence

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 said:
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
 
K

Ken Cox [Microsoft MVP]

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 said:
message 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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top