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" <fl************@hotmail.com> wrote in message
news:2C**********************************@microsof t.com...
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"),vbShortDa te)) %>' />
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