473,396 Members | 1,853 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,396 software developers and data experts.

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


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

Jun 24 '06 #1
4 2339
When you are using the IIF statement both the If and the Else clauses are
being processed. So when an exception occurs in your "Else" clause you will
still get an error even if it is the "If" clause that is invoked.

Use the DataGrid's ItemDataBound event to format the date instead using the
ordinary If Else statement.

Shawn

"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
Jun 24 '06 #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" <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

Jun 24 '06 #3

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospam> wrote in message
news:e5****************@TK2MSFTNGP03.phx.gbl...
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" <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


Jun 24 '06 #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" <fl************@hotmail.com> wrote in message
news:1D**********************************@microsof t.com...

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospam> wrote in
message news:e5****************@TK2MSFTNGP03.phx.gbl...
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" <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


Jun 25 '06 #5

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

Similar topics

1
by: Philip Mette | last post by:
I am in a crunch and need to covert this Oracle statement to MSSQL. Is there any Oracle/MSSQL experts out there that can help me? I do not understand the syntax enough to modify this. Thanks so...
3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
5
by: Steve | last post by:
Hello, I've been a PHP programmer for a number of years and have just started to learn JS. My Employer (a water analysis lab) wants what should be a very simple .js written that basically takes...
7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
3
by: Kevin Baker | last post by:
Hi Everyone, Need to find code to convert short date (1/20/2003) into Julian Date 3020. The "3" is last digit of year and 020 is the number of days since January 1st. Thanks, Kevin
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
4
by: Terry | last post by:
I have a TextBox with a date such as 15/01/2006 which I want to cast into a variable as a short date 15/01/06, also I need to cast a time such as 07:30 A.M. into a variable as a short time. What...
8
by: Stinky Pete | last post by:
OK, I am thoroughly confused. All I am trying to do is fill in a Status field on my form with the condtions below, with all my theory being based on what I have read in various newsgropups. The...
15
by: cephal0n | last post by:
I have a technical Date problem that's really difficult for me, I have a "custom made" Date format MM.DD.YY this is actually extracted from SAP and theirs no other format option offered such as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.