Visual Basic IsDBNull check crashing

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

The following code produces a "null error" if the SQL
database column is null:
If Not IsDBNull(DsUnit1.Unit(0).TechID) Then
cmbxTechFName.SelectedValue = DsUnit1.Unit
(0).TechID
cmbxTechLName.SelectedValue = DsUnit1.Unit
(0).TechID
End If
The TechID column is defined as a integer and can be
null. The error occurs on the If statement (ie. not the
code following the Then statement). Hope someone can
point me in the right direction!
 
Try this:
If (DsUnit1.Unit(0).TechID) = System.DBNull.Value Then
...

From .Net Help:
DBNull Class [Visual Basic]
This class is used to indicate the absence of a known
value, typically in a database application.

In database applications, a null object is a valid value
for a field. This class differentiates between a null
value (a null object) and an uninitialized value (the
DBNull.Value instance). For example, a table can have
records with uninitialized fields. By default, these
uninitialized fields have the DBNull value.
 
Bryan,
Thanks for the info ... I now am having a syntax
problem. Using your example the compiler doesn't like
the = operator (= operator is not defined for Integer and
System.DBnull). I also tried the is operator .. but got
another error(is requires operands to have ref types ..
this one has the value type integer)
Hope this makes sense ... appreciate your help ...
new .Net programmer
 
An easy check to see if it is Null, is

if "" & DsUnit1.Unit(0).TechID = "" Then

BUT looking at your example, are you sure that you are addressing column TechID? Is DsUnit1 a dataset? If so, shouldn't you use something like
DsUnit1.Tables(0).Rows(0)("TechID") to address the field? If not, then just forget the remark.
 
Chris,
Thanks for the help .. the DsUnit1.Tables(0).Rows(0)
("TechID") syntax worked. I've used my example syntax
before to address database columns, but it certainly
falls apart addressing null database columns ... thanks
again!
Dave
-----Original Message-----
An easy check to see if it is Null, is

if "" & DsUnit1.Unit(0).TechID = "" Then

BUT looking at your example, are you sure that you are
addressing column TechID? Is DsUnit1 a dataset? If so,
shouldn't you use something like
DsUnit1.Tables(0).Rows(0)("TechID") to address the
field? If not, then just forget the remark.
 
Back
Top