473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("ShippedDa te") %>

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("ShippedD ate")is
nothing,"My Text",Container .DataItem("Ship pedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No Date",Eval("Shi ppedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No
Date",FormatDat eTime(Eval("Shi ppedDate"),vbSh ortDate)) %>' />

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 2357
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******** *************** ***********@mic rosoft.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("ShippedDa te") %>

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("ShippedD ate")is
nothing,"My Text",Container .DataItem("Ship pedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No Date",Eval("Shi ppedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No
Date",FormatDat eTime(Eval("Shi ppedDate"),vbSh ortDate)) %>' />

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.dt d">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArg s)
If Not IsPostBack Then
Repeater1.DataS ource = CreateDataSourc e()
Repeater1.DataB ind()
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 CreateDataSourc e() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add( New Data.DataColumn _
("ShippedDat e", GetType(DateTim e)))
dt.Columns.Add( New Data.DataColumn _
("StringValu e", 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.V alue
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("S hippedDate")) %>'></asp:label></p>
</itemtemplate>
</asp:repeater>
</div>
</form>
</body>
</html>

"Jim Florence" <fl************ @hotmail.com> wrote in message
news:2C******** *************** ***********@mic rosoft.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("ShippedDa te") %>

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("ShippedD ate")is
nothing,"My Text",Container .DataItem("Ship pedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No Date",Eval("Shi ppedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No
Date",FormatDat eTime(Eval("Shi ppedDate"),vbSh ortDate)) %>' />

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**********@n ewsgroups.nospa m> wrote in message
news:e5******** ********@TK2MSF TNGP03.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.dt d">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArg s)
If Not IsPostBack Then
Repeater1.DataS ource = CreateDataSourc e()
Repeater1.DataB ind()
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 CreateDataSourc e() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add( New Data.DataColumn _
("ShippedDat e", GetType(DateTim e)))
dt.Columns.Add( New Data.DataColumn _
("StringValu e", 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.V alue
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("S hippedDate")) %>'></asp:label></p>
</itemtemplate>
</asp:repeater>
</div>
</form>
</body>
</html>

"Jim Florence" <fl************ @hotmail.com> wrote in message
news:2C******** *************** ***********@mic rosoft.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("ShippedDa te") %>

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("ShippedD ate")is nothing,"My
Text",Container .DataItem("Ship pedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No Date",Eval("Shi ppedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No
Date",FormatDat eTime(Eval("Shi ppedDate"),vbSh ortDate)) %>' />

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******** *************** ***********@mic rosoft.com...

"Ken Cox [Microsoft MVP]" <BA**********@n ewsgroups.nospa m> wrote in
message news:e5******** ********@TK2MSF TNGP03.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.dt d">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArg s)
If Not IsPostBack Then
Repeater1.DataS ource = CreateDataSourc e()
Repeater1.DataB ind()
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 CreateDataSourc e() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add( New Data.DataColumn _
("ShippedDat e", GetType(DateTim e)))
dt.Columns.Add( New Data.DataColumn _
("StringValu e", 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.V alue
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("S hippedDate")) %>'></asp:label></p>
</itemtemplate>
</asp:repeater>
</div>
</form>
</body>
</html>

"Jim Florence" <fl************ @hotmail.com> wrote in message
news:2C******** *************** ***********@mic rosoft.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("ShippedDa te") %>

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("ShippedD ate")is nothing,"My
Text",Container .DataItem("Ship pedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No Date",Eval("Shi ppedDate")) %>' />

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("ShippedDa te") ) is DbNull ,"No
Date",FormatDat eTime(Eval("Shi ppedDate"),vbSh ortDate)) %>' />

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
7029
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 much for any assistance. Here is the procedure. CREATE PROCEDURE UPD_ACTIVITY IS CURSOR ACT_cur1 IS SELECT DISTINCT A.ACCT_NUM, A.DUE_DATE
3
1729
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 write an if statement so that if: 1. The word "Month" is in the validFromMonth field (where they haven't selected a month a the drop-down menu) or the word "Year" is in the validFromYear field (where they haven't selected a year another the...
5
1871
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 sample hold time data from EPA regulations and spits out when a sample would expire, so we can properly label the thing. The problem is that the .js I have written appears to be doing something unexpected. The Analysis options are presented as...
7
11433
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 #11/2/2003# And VehicleID='00000000' And BattID='LKO500HF'. I need to use the records returned to populate text boxes, but the data requires further manipulation. I attempting to use expressions in the control source
3
10961
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
3843
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
2820
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 is the best syntax for this please? Regards
8
1981
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 IIf statement works in the forms Status field when I use the IIf wording as the control source. Obviously this is not saving into the field with the text I want but I found it useful as a check. On the form in the Status field, I've popped in...
15
4620
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 ShortDate, LongDate etc. so now I making a sql query that must convert SAP date to a date Format that is recognizable to MS Access and I choose the ShortDate MM/DD/YY using ADO. I read about the Format() command using ADO online and experiment on it and...
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.