I have a database where in column there is no value (so it's null), but I can't handle this in vb.net. I tried with this code:
reader.Read()
If String.IsNullOrEmpty(reader.GetString(0)) Then
Return
Else
tilbulfolderTextBox.Text = reader.GetString(0)
End If
and also with:
If reader.Read() = False Then
and with:
If IsDBNull(reader.Read()) Then
But apparently it doesn't work because I get exception on the statement after the Else that I can't get Null values with this method.
I guess you will figure out what I require from the program by reading the code itself.
The IsDBNull method of the DbDataReader base object is defined to handle this situation.
Of course you can't try to read something if the reader.Read() returns with false (meaning no more rows are available)
If reader.Read() Then
If reader.IsDBNull(0) Then
Return
Else
tilbulfolderTextBox.Text = reader.GetString(0)
End If
End If
Also, I don't see more of your code, but keep in mind that returning in this way could be very wrong if you don't close the connection and dispose the objects involved in this operation
And, yes, as others have pointed out, there is also a function called IsDBNull from the Microsoft.VisualBasic assembly, but, I prefer to use the methods provided by the classes defined in the .NET framework and not the ones provided for compatibility with previous versions of VB
You should use IsDbNull function to comapre it with null:
reader.Read()
If IsDbNull(reader(0)) OrElse String.IsNullOrEmpty(reader.GetString(0)) Then
Return
Else
tilbulfolderTextBox.Text = reader.GetString(0)
End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With