Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

retrieving data from a database smallint column that may be null

Posted on 2008-06-17
5
Medium Priority
?
482 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 2000 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

704 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