Solved

retrieving data from a database smallint column that may be null

Posted on 2008-06-17
5
476 Views
Last Modified: 2012-08-13
Hi

I don't program in c# so i hope you might be able to help me on this.

I need to retrieve data from a databse which is contained in a datareader. One of the columns in the reader, call it index 0, is of type smallint and it might have a value of null. what type of variable do i need to store this? Perhaps its nullable of int but i don't know how to use one of these in c#. Also how do i check the column value is not null before i retrieve it from the reader using something like myvar = reader.getInt16(0)

thanks a lot
andrea
0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 15

Accepted Solution

by:
crisco96 earned 500 total points
ID: 21804152
Null from a database is different from the .Net null.

Null from a database will actual be System.Data.DBNull.

If you want to save the value from the reader to a variable do the following:
int? Value;
 
if(Reader[0] == System.Data.DBNull)
{
   Value = null;
}
else
{
   Value = Convert.ToInt32(Reader[0]);
}

Open in new window

0
 

Author Comment

by:andieje
ID: 21804394
hi, i tried that and got this error

Invalid attempt to read from column ordinal '4'.  With CommandBehavior.SequentialAccess, you may only read from column ordinal '5' or greater.
  if (reader[4] != DBNull.Value)
                            {
                               
                                subcontractorID = Convert.ToInt16(reader[4]);
                            }
                            else
                            {
                                subcontractorID = null;
                            }
                         
0
 

Author Comment

by:andieje
ID: 21804457
I CHANGED IT to this
   if (reader["subcontractorID"] != DBNull.Value)
                            {
                               
                                subcontractorID = Convert.ToInt16(reader[4]);
                            }
                            else
                            {
                                subcontractorID = null;
                            }
                         

thanks
0
 

Author Closing Comment

by:andieje
ID: 31467992
thanks
0
 
LVL 5

Expert Comment

by:Hendo1973
ID: 21804474
You could use Nullable types which would allow value types to have a null value assigned to them.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question