import-bot (20211) [Avatar] Offline
#1
[Originally posted by awiggans]

I am using a data reader with the following line of code:

ab.Company = (System.Int32)(reader[i]);

In my database, I am reading a null value from the
database and am receiving a Invalid Cast Exception--most
likly because I am trying to assign a null value to a int.

How should I write the above code to check for a null
before trying to assign--or any other way to avoid the
invalid cast exception.

Thanks. Aaron
import-bot (20211) [Avatar] Offline
#2
Re: data reader-Invalid Cast Exception with a null db value
[Originally posted by awiggans]

I found the answer:

if (reader.IsDBNull(0))
Company = "";
else
Company = reader.GetString(0);
import-bot (20211) [Avatar] Offline
#3
Re: data reader-Invalid Cast Exception with a null db value
[Originally posted by arlen]


Sorry I didn't reply sooner, but your solution is exactly right!