Solved

retrieving data from a database smallint column that may be null

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

17 Experts available now in Live!

Get 1:1 Help Now