Solved

retrieving data from a database smallint column that may be null

Posted on 2008-06-17
5
468 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A short film showing how OnPage and Connectwise integration works.

932 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now