how to deal with null value from database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Whwn I select from database using ADO.NET, if the data is null value in database, how should I handle it
(string, integer, boolean type field)

Thank

willia
 
You should check if it a null. You can use the IsDBNull function in VB. The
DataRow has an IsNull method, and the data reader an IsDBNull method, that
can be used from any language, since they're just methods on those classes.

If it turns out the value is a null - then you must handle that
appropriately. Otherwise, you can cast/convert the value to the appropriate
datatype.

william said:
Hi,

Whwn I select from database using ADO.NET, if the data is null value in
database, how should I handle it?
 
william said:
Hi,

Whwn I select from database using ADO.NET, if the data is null value
in database, how should I handle it? (string, integer, boolean type
field).

The value is System.DBNull. You can check for it by using static bool
Convert.IsDBNullI(object).

--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
Checking using IsDBNull is a universal solution. But if you are dealing with
only strings, just append an empty string.

Eg., curRow("StrinColumn1") & ""

This will convert the null value to empty string and thus error is avoided.
I am using this technique successfully from VB3 days when only dealing with
string columns. This is simple and executes faster as no function call is
involved.

ThanQ...


william said:
Hi,

Whwn I select from database using ADO.NET, if the data is null value in
database, how should I handle it?
 
Shanti said:
Checking using IsDBNull is a universal solution. But if you are dealing with
only strings, just append an empty string.

Eg., curRow("StrinColumn1") & ""

This will convert the null value to empty string and thus error is avoided.
I am using this technique successfully from VB3 days when only dealing with
string columns. This is simple and executes faster as no function call is
involved.

You don't need a function call if you just check whether the value is
an instance of DBNull either (if (value is DBNull) in C#). The problem
with your way of doing things is that it treats empty strings as nulls
as well, when they're not. You may well want to distinguish between the
two.
 
Back
Top