Using VB.Net
I want to check whether the table row is null or 0
Code
sCmd = New SqlCommand("SELECT * from table1)", conObjects.myConnection)
dReader = sCmd.ExecuteReader
While dReader.Read()
If dReader.Item(11) <> "0" And dReader.Item(11) IsNot Nothing Then
msgbox ("Not NUll or 0")
End if
End while
Showing Error as "Operator '<>' is not defined for type 'DBNull' and string "0""
Why i am getting this error, How to solve this problem.
Need VB.Net Code Help
You should always use AndAlso and OrElse instead of And and Or.
With AndAlso the latter expression is only evaluted if the former returned true. On this way you can avoid exceptions and make your code more efficient.
But apart from that, you should use reader.IsDBNull(index):
If Not dReader.IsDBNull(11) AndAlso dReader.GetString(11) <> "0" Then
End If
There are two problems here: one is that you use And instead of AndAlso: the former will cause your whole line to be evaluated, where the latter will only evaluate the second argument if its first argument is already True.
Your second problem is that you check for "not zero" before you check for "Nothing", and so your first part can fail before the second part is evaluated.
Try this instead:
If dReader.Item(11) IsNot Nothing AndAlso dReader.Item(11) <> "0" Then
Also, it's been a while since I did ADO, but shouldn't you be checking IsDbNull instead of comparing to Nothing? Perhaps VB takes care of that for you, I'm not sure.
Try using AndAlso
If (dReader.Item(11) IsNot Nothing) AndAlso (cint(dReader.Item(11)) <> 0) Then
msgbox ("Not NUll or 0")
End if
basically what it does is, when the first expression is already true, the second expression is not evaluated.
If Convert.ToString(dReader.Item(11)) <> "0" AndAlso dReader.IsDBNull(11)=False
Related
This question already has answers here:
Single statement across multiple lines in VB.NET without the underscore character
(8 answers)
Closed 4 years ago.
I apologize if this is documented somewhere, I could not find the answer using an underscore in this specific manner (an If Not, AndAlso statement).
Here is what I don't understand
If Not variable Is Nothing _
AndAlso Not Results.Tables.Count = 0 _
AndAlso Not Results.Tables(0).Rows.Count = 0 Then
If I take away the Underscores in this code, VB2017 automatically puts a Then at the end of the first line of code and I get 2 Syntax Errors for AndAlso on both lines they are used. Can someone explain why this is? I read on StackOverflow that the Compiler doesn't care about the underscore, and I've also read MSDN's AndAlso documentation and I'm a little confused.
Thank you for your help.
I read on StackOverflow that the Compiler doesn't care about the underscore
That's not quite true. Implicit line continuations are a fairly recent addition to the Visual Basic language1. They cover many, but not all, scenarios where the _ line continuation character used to be needed.
Here's the complete list from linked documentation:
After a comma (,).
After an open parenthesis (() or before a closing parenthesis ()).
After an open curly brace ({) or before a closing curly brace (}).
After an open embedded expression (<%=) or before the close of an embedded expression (%>) within an XML literal.
After the concatenation operator (&). For example:
After assignment operators (=, &=, :=, +=, -=, *=, /=, \=, ^=, <<=, >>=).
After binary operators (+, -, /, *, Mod, <>, <, >, <=, >=, ^, >>, <<, And, AndAlso2, Or, OrElse, Like, Xor) within an expression.
After the Is and IsNot operators.
After a member qualifier character (.) and before the member name.
This shows we can safely add lines after AndAlso rather than before:
If Not variable Is Nothing AndAlso
Not Results.Tables.Count = 0 AndAlso
Not Results.Tables(0).Rows.Count = 0 Then
I also suggest rewriting as follows, because VB.Net does odd things with Not, using it as a bitwise rather than logical operator3:
If variable IsNot Nothing AndAlso
Results.Tables.Count > 0 AndAlso
Results.Tables(0).Rows.Count > 0 Then
1. Okay, 2010, but I still find a lot of VB developers who don't know about them, or know about them but never check the rules and either avoid them or let the compiler tell them when to add one.
2. Emphasis mine.
3. Plus, I just find it easier to read without the negations.
"if I make it all one line of code I still get a Syntax Error"
This compiles fine:
If Not variable Is Nothing AndAlso Not Results.Tables.Count = 0 AndAlso Not Results.Tables(0).Rows.Count = 0 Then Exit Sub
This also compiles fine:
If Not variable Is Nothing _
AndAlso Not Results.Tables.Count = 0 _
AndAlso Not Results.Tables(0).Rows.Count = 0 Then Exit Sub
What won't work is this:
If Not variable Is Nothing
AndAlso Not Results.Tables.Count = 0
AndAlso Not Results.Tables(0).Rows.Count = 0 Then Exit Sub
Without the _ underscore it expects the If statement to be fully formed on the same line.
As Joel points out, the implicit line continuations require the AndAlso before the line break. See his better answer.
What's good to know however is that the underscores are not necessary when using Linq in newer versions of the compiler:
Dim interesting = From r As DataRow In Results.Tables(0).AsEnumerable()
Let fieldId = r.Field(Of Integer)("id")
Where fieldId <> 0
Order By fieldId
Select r
It means "line continues on next line".
You can also write it as:
If variable IsNoT Nothing AndAlso
Results.Tables.Count > 0 AndAlso
Results.Tables(0).Rows.Count > 0 Then
'something
End If
The conditions in the If statement must be on one line as a VB standard. because some conditions might be too long to be readable on one line, the underscore is used as an escape character that indicates that the line is not finished.
If you write
If Not variable Is Nothing
AndAlso Not Results.Tables.Count = 0
AndAlso Not Results.Tables(0).Rows.Count = 0 Then
Visual stdio will only consider the first line as part of the If statement Making the result:
If Not variable Is Nothing Then
AndAlso Not Results.Tables.Count = 0
AndAlso Not Results.Tables(0).Rows.Count = 0 Then
You will need to put all conditions on one line or use the underscore.
This is the standard in VB.
I have seen similar questions and as I have to work with VB.net for a project I am stuck with fairly simple thing.
If (myValue IsNot Nothing) And (myValue.Length > 12) Then
' do some stuff
End If
Now when myValue is Nothing it gives me null reference exception. I have also tried,
If (Not myValue Is Nothing) And (myValue.Length > 12) Then
' do some stuff
End If
In C# which I am very much used it is very easy but in VB.net I am not finding nay way.
Use AndAlso instead of And (and OrElse instead of Or).
AndAlso performs a short-circuiting logical conjunction on two expressions. And
will evaluate both even if the first already returned False.
Side-note: you should also use the If-operator instead of the old VB function IIf for the same reason.
I have a statement in VB.net that I thought I wrote correctly to prevent the second half from being evaluated. It looks like this:
If ((myDataSet2 IsNot Nothing) Or myDataSet2.Tables("CurData").Rows.Count > 0)
However it does not skip the second expresion "myDataSet2.Tables("CurData").Rows.Count > 0" like I want it to.
What should I change?
Use the OrElse operator.
If myDataSet2 IsNot Nothing OrElse myDataSet2.Tables("CurData").Rows.Count > 0
EDIT: See my comment on your original question. You are PROBABLY looking for:
If myDataSet2 IsNot Nothing AndAlso myDataSet2.Tables("CurData").Rows.Count > 0
That will check if myDataSet2 isn't null. Assuming that it's not, it will then check that there is at least one row. If it is null, then the second condition won't be checked.
You need to put the second statement into the first if-clause.
Like this:
If(statement1) then
If(statemtent2) then
Else
End if
Else
End If
As it is now both are evaluated and if one of them is true the content in your if-clause will be executed.
I need to make something like :
if isdbnull(value) or value = something then
'do something
else
'do something else
end if
of course i get an error using this method , so my question is how do i rewrite it to avoid the "operator not defined for dbnull and something" error ?
There are a few approaches to this, and which you use may depend on the values you're working with. However, if you want something that catches all conditions then I'd do this:
If Value Is Nothing OrElse IsDbNull(value) Then
'do something
Else
'do something else
End If
This will check if the Value is nothing, which can sometimes happen without the value actually being DBNull.
But the most important part of this is the OrElse. OrElse is the short-circuiting operator which terminates the evaluation of the condition as soon as the runtime knows what the outcome will be. By contrast, the Or operator will execute the entire condition no matter what, and that is the reason your original code fails.
EDIT:
Now that I look at your example code again, I can see how my NotNull() function may help you:
Public Shared Function NotNull(Of T)(ByVal Value As T, ByVal DefaultValue As T) As T
If Value Is Nothing OrElse IsDBNull(Value) Then
Return DefaultValue
Else
Return Value
End If
End Function
Usage:
if NotNull(value, something) = something then
'do something
else
'do something else
end if
I tend to use the OrElse, AndAlso operators
If IsDBnull(value) OrElse value = something Then
''#do something
Else
''#do something else
End If
The shortcuts operator means the rest of the If conditions won't be executed if the first is true.
Edit: Steve beat me to it while I was formatting my answer :)
value Is DBNull.Value
value is nothing
In VB.NET, what is the difference between And and AndAlso? Which should I use?
The And operator evaluates both sides, where AndAlso evaluates the right side if and only if the left side is true.
An example:
If mystring IsNot Nothing And mystring.Contains("Foo") Then
' bla bla
End If
The above throws an exception if mystring = Nothing
If mystring IsNot Nothing AndAlso mystring.Contains("Foo") Then
' bla bla
End If
This one does not throw an exception.
So if you come from the C# world, you should use AndAlso like you would use &&.
More info here: http://www.panopticoncentral.net/2003/08/18/the-ballad-of-andalso-and-orelse/
The And operator will check all conditions in the statement before continuing, whereas the Andalso operator will stop if it knows the condition is false. For example:
if x = 5 And y = 7
Checks if x is equal to 5, and if y is equal to 7, then continues if both are true.
if x = 5 AndAlso y = 7
Checks if x is equal to 5. If it's not, it doesn't check if y is 7, because it knows that the condition is false already. (This is called short-circuiting.)
Generally people use the short-circuiting method if there's a reason to explicitly not check the second part if the first part is not true, such as if it would throw an exception if checked. For example:
If Not Object Is Nothing AndAlso Object.Load()
If that used And instead of AndAlso, it would still try to Object.Load() even if it were nothing, which would throw an exception.
Interestingly none of the answers mentioned that And and Or in VB.NET are bit operators whereas OrElse and AndAlso are strictly Boolean operators.
Dim a = 3 OR 5 ' Will set a to the value 7, 011 or 101 = 111
Dim a = 3 And 5 ' Will set a to the value 1, 011 and 101 = 001
Dim b = 3 OrElse 5 ' Will set b to the value true and not evaluate the 5
Dim b = 3 AndAlso 5 ' Will set b to the value true after evaluating the 5
Dim c = 0 AndAlso 5 ' Will set c to the value false and not evaluate the 5
Note: a non zero integer is considered true; Dim e = not 0 will set e to -1 demonstrating Not is also a bit operator.
|| and && (the C# versions of OrElse and AndAlso) return the last evaluated expression which would be 3 and 5 respectively. This lets you use the idiom v || 5 in C# to give 5 as the value of the expression when v is null or (0 and an integer) and the value of v otherwise. The difference in semantics can catch a C# programmer dabbling in VB.NET off guard as this "default value idiom" doesn't work in VB.NET.
So, to answer the question: Use Or and And for bit operations (integer or Boolean). Use OrElse and AndAlso to "short circuit" an operation to save time, or test the validity of an evaluation prior to evaluating it. If valid(evaluation) andalso evaluation then or if not (unsafe(evaluation) orelse (not evaluation)) then
Bonus: What is the value of the following?
Dim e = Not 0 And 3
If Bool1 And Bool2 Then
Evaluates both Bool1 and Bool2
If Bool1 AndAlso Bool2 Then
Evaluates Bool2 if and only if Bool1 is true.
Just for all those people who say side effects are evil: a place where having two side effects in one condition is good would be reading two file objects in tandem.
While File1.Seek_Next_Row() And File2.Seek_Next_Row()
Str1 = File1.GetRow()
Str2 = File2.GetRow()
End While
Using the And ensures that a row is consumed every time the condition is checked. Whereas AndAlso might read the last line of File1 and leave File2 without a consumed line.
Of course the code above wouldn't work, but I use side effects like this all the time and wouldn't consider it "bad" or "evil" code as some would lead you to believe. It's easy to read and efficient.
AndAlso is much like And, except it works like && in C#, C++, etc.
The difference is that if the first clause (the one before AndAlso) is true, the second clause is never evaluated - the compound logical expression is "short circuited".
This is sometimes very useful, e.g. in an expression such as:
If Not IsNull(myObj) AndAlso myObj.SomeProperty = 3 Then
...
End If
Using the old And in the above expression would throw a NullReferenceException if myObj were null.
A simple way to think about it is using even plainer English
If Bool1 And Bool2 Then
If [both are true] Then
If Bool1 AndAlso Bool2 Then
If [first is true then evaluate the second] Then
Also see Stack Overflow question: Should I always use the AndAlso and OrElse operators?.
Also: A comment for those who mentioned using And if the right side of the expression has a side-effect you need:
If the right side has a side effect you need, just move it to the left side rather than using "And". You only really need "And" if both sides have side effects. And if you have that many side effects going on you're probably doing something else wrong. In general, you really should prefer AndAlso.
In addition to the answers above, AndAlso provides a conditioning process known as short circuiting. Many programming languages have this functionality built in like vb.net does, and can provide substantial performance increases in long condition statements by cutting out evaluations that are unneccessary.
Another similar condition is the OrElse condition which would only check the right condition if the left condition is false, thus cutting out unneccessary condition checks after a true condition is found.
I would advise you to always use short circuiting processes and structure your conditional statements in ways that can benefit the most by this. For example, test your most efficient and fastest conditions first so that you only run your long conditions when you absolutely have to and short circuit the other times.
For majority of us OrElse and AndAlso will do the trick except for a few confusing exceptions (less than 1% where we may have to use Or and And).
Try not to get carried away by people showing off their boolean logics and making it look like a rocket science.
It's quite simple and straight forward and occasionally your system may not work as expected because it doesn't like your logic in the first place. And yet your brain keeps telling you that his logic is 100% tested and proven and it should work. At that very moment stop trusting your brain and ask him to think again or (not OrElse or maybe OrElse) you force yourself to look for another job that doesn't require much logic.
Use And and Or for logical bit operations, e.g. x% = y% Or 3
AndAlso and OrElse are for If statements:
If x > 3 AndAlso x <= 5 Then
is same as
If (x > 3) And (x <= 5) Then
The way I see it...
Also it's worth noting that it's recommended (by me) that you enclose equations with logical operators in If statements, so they don't get misinterpreted by the compiler, for example:
If x = (y And 3) AndAlso ...
To understand with words not cods:
Use Case:With “And” the compiler will check all conditions so if you are checking that an object could be “Nothing” and then you are checking one of it’s properties you will have a run time error.
But with AndAlso with the first “false” in the conditions it will checking the next one so you will not have an error.
The And / AndAlso Or / OrElse are actually quite useful. Consider this code, where the function DoSomethingWihtAandB may not set A and/or B if it returns False:
Dim A,B,C,D As Object
If DoSomethingWithAandB(A,B)=True And A=1 And B=2 Then C=3
It will crash if DoSomethingWithAandB returns False, because it will continue to evaluate after the And and A and B will equal Nothing
Dim A,B,C,D As Object
If DoSomethingWithAandB(A,B)=True AndAlso A=1 AndAlso B=2 Then C=3
This won't crash if DoSomethingWithAandB returns False because it will stop evaluating at DoSomethingWithAandB(A,B)=True, because False is returned. The AndAlso prevents any further evaluation of the conditions (as the first condition failed). OrElse works the same way. The first True evaluated in the logic chain will stop any further evaluation.