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

Comparison problem

Hi

I am trying to see if column forenames is empty/blank in a row in a
datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string "".'
error on the above line. What am I doing wrong?

Thanks

Regards

Nov 20 '05 #1
12 3649
You can not use the "=" operator for checking DBNull. Instead, use:

If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then

-Scott
"John" <jo**@nospam.infovis.co.uk> wrote in message
news:OY****************@TK2MSFTNGP11.phx.gbl...
Hi

I am trying to see if column forenames is empty/blank in a row in a
datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string "".'
error on the above line. What am I doing wrong?

Thanks

Regards

Nov 20 '05 #2
As Scott suggested:

If (mydatatable.Rows.Item(0).Item("Forenames") = System.DBNull.Value) Then

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"John" <jo**@nospam.infovis.co.uk> wrote in message
news:OY****************@TK2MSFTNGP11.phx.gbl...
Hi

I am trying to see if column forenames is empty/blank in a row in a
datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string "".'
error on the above line. What am I doing wrong?

Thanks

Regards

Nov 20 '05 #3
"John" <jo**@nospam.infovis.co.uk> schrieb

I am trying to see if column forenames is empty/blank in a row in
a datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string
"".' error on the above line. What am I doing wrong?


If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #4
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then (suggested by
Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") = System.DBNull.Value)
Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then
(suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker than
another?)

Paw

"Armin Zingler" <az*******@freenet.de> wrote in message
news:O6**************@TK2MSFTNGP10.phx.gbl...
"John" <jo**@nospam.infovis.co.uk> schrieb

I am trying to see if column forenames is empty/blank in a row in
a datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string
"".' error on the above line. What am I doing wrong?


If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
Hi Paw,

"Paw Boel Nielsen" <no*@important.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then (suggested by Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") = System.DBNull.Value)
Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then (suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker than
another?)


My is the fastest (2) beacuse it is a simple comparision while (3) is
probably the slowest because of type checking involved.
(1) is in the middle - it is slower than mine because it is a method and it
also checks the value agains null.

My humble opinion :)

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
Nov 20 '05 #6
Cor
Armin,

Is this datatable really working

If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then


The only thing I can understand it is:

If it is a dataset with the name "mydatatable" that has a table with the
name "Rows"

Where is my mistake

:-)

Cor
Nov 20 '05 #7
"Paw Boel Nielsen" <no*@important.com> schrieb
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then
(suggested by Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value) Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is
System.DBNull.Value Then (suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker
than another?)


4) If TypeOf mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull
Solution 1) does the same as 4), so 4) is faster because no function call is
involved.

2) is wrong because references have to be compared using the Is operator
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8
"Miha Markic" <miha at rthand com> schrieb
Hi Paw,

"Paw Boel Nielsen" <no*@important.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then
(suggested by
Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value) Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is
System.DBNull.Value

Then
(suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker
than another?)


My is the fastest (2) beacuse it is a simple comparision


References have to be compared using the Is operator.
2) leads to a compiler error. Sorry. ;-)
while (3)
is probably the slowest because of type checking involved.
No, it does not check the types, it only compares references.
(1) is in the middle - it is slower than mine because it is a method
and it also checks the value agains null.

My humble opinion :)

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
"Cor" <no*@non.com> schrieb
Armin,

Is this datatable really working

If mydatatable.Rows.Item(0).Item("Forenames") Is
System.DBNull.Value Then


The only thing I can understand it is:

If it is a dataset with the name "mydatatable" that has a table with
the name "Rows"

Where is my mistake

:-)


??

I'd say:
If the field called "forenames" in the first row of the datable named
"mydatatable" references DBNull.Value then

:-)

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #10
Hi Armin,

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
"Paw Boel Nielsen" <no*@important.com> schrieb
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then
(suggested by Scott M.)
4) If TypeOf mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull
Solution 1) does the same as 4), so 4) is faster because no function call is involved.
Yes, it is faster and no it does not the same thing. 1) checks also if value
is null.
Beware that if 1) and 4) do the same then they could be equal in terms of
speed if JIT optimizes 1) to inline calls.
2) is wrong because references have to be compared using the Is operator


Yeah, yeah, VB.NET.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
Nov 20 '05 #11
1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then
(suggested

by
Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value) Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is
System.DBNull.Value

Then
(suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker
than another?)


My is the fastest (2) beacuse it is a simple comparision


References have to be compared using the Is operator.
2) leads to a compiler error. Sorry. ;-)


You sure on this?
Actually you are right - I've written it in C# and then not complectly
rewritten in VB.NET. Duh.

while (3)
is probably the slowest because of type checking involved.


No, it does not check the types, it only compares references.


Right. I was thinking in C# terms.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
Nov 20 '05 #12
"Miha Markic" <miha at rthand com> schrieb
Hi Armin,

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
"Paw Boel Nielsen" <no*@important.com> schrieb
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames" ) Then
(suggested by Scott M.)
4) If TypeOf mydatatable.Rows.Item(0).Item("Forenames") Is
System.DBNull
Solution 1) does the same as 4), so 4) is faster because no
function call is
involved.


Yes, it is faster and no it does not the same thing. 1) checks also
if value is null.


Right. I meant "do the same" in the sense of "both compare the types"
(whereas the other one compares references)
Beware that if 1) and 4) do the same then they could be equal in
terms of speed if JIT optimizes 1) to inline calls.


Well, if a function does the same as the same code does outside the
function, they do the same. Of course, I agree, the function call itself
takes additional time.
2) is wrong because references have to be compared using the Is
operator


Yeah, yeah, VB.NET.


:-)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #13

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

Similar topics

0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
10
by: chandra.somesh | last post by:
Hi I recently had to write a small code in a competition ,but my code was rejected cause it failed in 1 of test cases. The problm was .....we are given vector of strings....each string...
29
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
3
by: deltauser2006 | last post by:
My database consists of information which is updated every quarter. Forms will compare data from the present quarter to quarters past. I need a way to make the database save a copy of itself every...
6
by: sales | last post by:
Hello, I am trying to get my website checkout page to rotate / take turns displaying shopping comparison engine surveys rather than display them all 4 at the same time, thus overwhelming &...
3
by: Alex | last post by:
I have a problem about the comparison between signed and unsigned integer. See the following code: int foo() { int i; unsigned int j; if (i < j) {
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.