Null value

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

I AM A NEWBIE. ANy help appreciated

The following statement crashes when the database entry is dbNUll How
do I prevent it

Name = ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1)
 
I AM A NEWBIE. ANy help appreciated

The following statement crashes when the database entry is dbNUll How
do I prevent it

Name = ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1)

Hi

you can use these two methods..

Name =
ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1).Tostring()

or

if
isDBNull(ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1))=false then

Name =
ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1).Tostring()

end if
 
I AM A NEWBIE. ANy help appreciated

The following statement crashes when the database entry is dbNUll How
do I prevent it

Name = ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1)

What I find that works is
if ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1) = "" then
Name = "Empty"
Else
Name = ds.Tables(0).Rows.Item(i).ItemArray.GetValue(1)
End if

What is wierd in .net 2.0 i sthat isdbnull or isnull don;t work as
well!
If someone can elaborate....

Tom Bizannes
Sydney,Australia
 
First of all, a little pointer because you are making life difficult for
yourself.

The Item property of the Rows collection of a DataTable is the default
property so you don't have to code:

ds.Tables(0).Rows.Item(i)

Rather, you can simply code:

ds.Tables(0).Rows(i)

Similarly, the default property of a DataRow is DataColumns (or Columns) and
the default property of a DataColumn (or Column) is Value. Therefore it is
simpler to code:

Name = ds.Tables(0).Rows(i)(1)

Assuming Name is a String, then because the Value property returns an Object
you will need to cast it accordingly:

Name = CType(ds.Tables(0).Rows(i)(1), String)

Now this is where DbNull's rear their ugly head because you can't cast a
DbNull to a String (or any other type for that matter.

What you have to do is determine your business rule for handling DbNull.
If, for instance, you decide that (for a String) dbNull should beconverted
to an empty string then you can use:

Name = String.Empty

If Not IsDbNull(ds.Tables(0).Rows(i)(1)) Then Name =
CType(ds.Tables(0).Rows(i)(1), String)

As a variation you couild could also use:

If ds.Tables(0).Rows(i)(1) <> DbNull.Value Then Name =
CType(ds.Tables(0).Rows(i)(1), String)

That is really a matter of personal preference but whichever you use it must
fit in your comfort zone.

For Tom, the IsDbNull certainly does work in Framework 2.0 but the IsNull
function will certainly NOT work in relation to DbNull's.

The secret is to make sure you learn the difference between DbNull.Value and
Nothing which are completely different things.
 
The secret is to make sure you learn the difference between DbNull.Value
and Nothing which are completely different things.
A DbNull.Value means a not with a 'value' filled column in a database or any
other data collection that expects a value.
Nothing means a not with a 'reference' filled address in your program.

Cor
 
Hi Stephany,

Just a little question, why don't you use the most base method ToString here
or even the VB function Cstr.
The result will be the same however it is less typing.

Cor
 
Personal preference.

If saving a few keystrokes here and there was my biggest issue then I would
be very happy indeed.
 
Back
Top