transition a null value in database into a code

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

Guest

Hi,

Imagine that we have a database and one table. Stored procedure returns 3
columns of table: ID, name, value.
Name and value can be a null in database. How to map or represent such
values in .net code? When I map a null value to a string or double variable I
get an error.

I have heard that one of solutions can be converting and representing
db.null as a minimum value of used type. Is it a good technic? And what in
situaltion when I need all the values variable can represent from minimum to
maximum?

Thanks.
Przemo
 
Przemo,

If you are using .NET 2.0, you can use the nullable type.

When you are dealing with your data set though, you can use the return
value from the Value property on DBNull to indicate null in a database.

Hope this helps.
 
Hi,

before assigning the value readed from the DB you can check for
DBNull.Value , if true then you have to decide what to do, possibles options
include:

1- In case of values types assign a especific value, like Int32.MinValue,
Int32.MaxValue, etc
1.a - In references types, you can always assign null and later check for
it, or you can use a "default" instance

2- In case that all the values of a type are possible (like bool) you have
to define a wrapper, with two members, the value and a flag that indicate if
the value exist or not

3- Upgrade to 2.0 that include a nullable type


cheers,
 
Another option to consider is having your database do some of the work
for you. You may have a SQL command like NVL, that will return the
value for the column if it isn't null or some default value if it is
null.

Example:
SELECT fname, NVL (addr, 'Address unknown') AS address
FROM employees

(This is the Informix NVL syntax)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top