469,299 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

IIF Function

I am using the IIF function in my application to create an instance and
value of a field if it doesn't exist in the query. I am using a
datagrid to display 4-5 items at a time and if one of the items doesn't
have a revision listed in tblRevisions then the whole page errors out.
I am using the following to try and fix the problem:

Dim intADCN As Integer =
IIf(ds1.Tables("DataTable").Rows(0)("RevNumber") Is Nothing, 0,
ds.Tables("DataTable").Rows(0)("RevNumber"))

I am getting an error: Cast from type 'DBNull' to type 'Integer' is not
valid. It seems as though instead of creating the instance and value of
0, it is simply creating the instance of there being an a row there and
the value is null.

Am I missing something or does anyone have any suggestions?

Nov 19 '05 #1
5 3923
Taken from your example, this will return 0 if the value is Null, else the
value:
Dim intADCN As Integer = "0" & ds1.Tables("DataTable").Rows(0)("RevNumber")
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I am using the IIF function in my application to create an instance and
value of a field if it doesn't exist in the query. I am using a
datagrid to display 4-5 items at a time and if one of the items doesn't
have a revision listed in tblRevisions then the whole page errors out.
I am using the following to try and fix the problem:

Dim intADCN As Integer =
IIf(ds1.Tables("DataTable").Rows(0)("RevNumber") Is Nothing, 0,
ds.Tables("DataTable").Rows(0)("RevNumber"))

I am getting an error: Cast from type 'DBNull' to type 'Integer' is not
valid. It seems as though instead of creating the instance and value of
0, it is simply creating the instance of there being an a row there and
the value is null.

Am I missing something or does anyone have any suggestions?

Nov 19 '05 #2
I fixed it!

Dim intRevNumber As Integer

If ds.Tables("DataTable").Rows(0)("RevNumber") Is DBNull.Value Then
intRevNumber = 0
Else
intRevNumber = ds.Tables("DataTable").Rows(0)("RevNumber")
End If

Nov 19 '05 #3
Pat
Good Buddy

"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I fixed it!

Dim intRevNumber As Integer

If ds.Tables("DataTable").Rows(0)("RevNumber") Is DBNull.Value Then
intRevNumber = 0
Else
intRevNumber = ds.Tables("DataTable").Rows(0)("RevNumber")
End If

Nov 19 '05 #4
> Dim intADCN As Integer = "0" &
ds1.Tables("DataTable").Rows(0)("RevNumber")
Turning Option Strict Off is extremely bad practice, and the above code is a
horrible example of why. You're treating BOTH a string and possibly Nothing
as an Integer. You're also occasionally concatenating Nothing to a String.
This sort of coding can lead to all kinds of debugging headaches, as well as
not throwing exceptions that should be thrown, thus enabling your app to
pass the smell test in testing, and fail behind your back in production.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Chris Botha" <ch***********@AThotmail.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl... Taken from your example, this will return 0 if the value is Null, else the
value:
Dim intADCN As Integer = "0" &
ds1.Tables("DataTable").Rows(0)("RevNumber")
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I am using the IIF function in my application to create an instance and
value of a field if it doesn't exist in the query. I am using a
datagrid to display 4-5 items at a time and if one of the items doesn't
have a revision listed in tblRevisions then the whole page errors out.
I am using the following to try and fix the problem:

Dim intADCN As Integer =
IIf(ds1.Tables("DataTable").Rows(0)("RevNumber") Is Nothing, 0,
ds.Tables("DataTable").Rows(0)("RevNumber"))

I am getting an error: Cast from type 'DBNull' to type 'Integer' is not
valid. It seems as though instead of creating the instance and value of
0, it is simply creating the instance of there being an a row there and
the value is null.

Am I missing something or does anyone have any suggestions?


Nov 19 '05 #5
I've been doing this since the days of VB6 and ADO, no problems yet.
Thanks for the comments though.

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:ec**************@TK2MSFTNGP15.phx.gbl...
Dim intADCN As Integer = "0" &
ds1.Tables("DataTable").Rows(0)("RevNumber")


Turning Option Strict Off is extremely bad practice, and the above code is
a horrible example of why. You're treating BOTH a string and possibly
Nothing as an Integer. You're also occasionally concatenating Nothing to a
String. This sort of coding can lead to all kinds of debugging headaches,
as well as not throwing exceptions that should be thrown, thus enabling
your app to pass the smell test in testing, and fail behind your back in
production.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

"Chris Botha" <ch***********@AThotmail.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Taken from your example, this will return 0 if the value is Null, else
the value:
Dim intADCN As Integer = "0" &
ds1.Tables("DataTable").Rows(0)("RevNumber")
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I am using the IIF function in my application to create an instance and
value of a field if it doesn't exist in the query. I am using a
datagrid to display 4-5 items at a time and if one of the items doesn't
have a revision listed in tblRevisions then the whole page errors out.
I am using the following to try and fix the problem:

Dim intADCN As Integer =
IIf(ds1.Tables("DataTable").Rows(0)("RevNumber") Is Nothing, 0,
ds.Tables("DataTable").Rows(0)("RevNumber"))

I am getting an error: Cast from type 'DBNull' to type 'Integer' is not
valid. It seems as though instead of creating the instance and value of
0, it is simply creating the instance of there being an a row there and
the value is null.

Am I missing something or does anyone have any suggestions?



Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
5 posts views Thread by phil_gg04 | last post: by
2 posts views Thread by laredotornado | last post: by
2 posts views Thread by sushil | last post: by
8 posts views Thread by Olov Johansson | last post: by
3 posts views Thread by Beta What | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.