[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Integer cast DataRow value???

Posted on 2006-05-16
10
Medium Priority
?
3,558 Views
Last Modified: 2008-02-01
While the program compiles correctly, I still recieve an error concerning "Object cannot be cast from DbNull to other types".

The SQL 2000 database column "Day" is set to Integer.

I would like to see VS C# 2005 allow this:   int intDay = dr["Day"].ToInt16();

However, this is what I have to work with?

 foreach (DataRow dr in dataset1.Tables["Sales"].Rows)
 {
      intDay = Convert.ToInt16(dr["Day"]);
 }
0
Comment
Question by:kvnsdr
10 Comments
 
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 16691230
change
intDay = Convert.ToInt16(dr["Day"]);

to
intDay = dr["Day"]==DBnull.Value?0:Convert.ToInt16(dr["Day"]);
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16691309
There is no method ToInt16(); in the object class

0
 
LVL 23

Expert Comment

by:apresto
ID: 16691483
try

short.Parse(dr["Day"].ToString());
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:mrichmon
ID: 16691502
While Desp's comment will solve the immediate problem, you may want to consider what is causing the problem.  If you are using .NET 1.1 then you must use a "magic number" or default value if null is encountered.  But consider: should the column ever be allowed to be null?

If you are running .NET 2 you may want to account for that by using a nullable type.  You do this by adding a ? at the end of the type in the declaration.

int? intDay

Otherwise you may want to ensure that your database does not allow null in this field.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 16693514
That was my first thought, so I made sure all rows in that column had a number. However, the error persists.........
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 750 total points
ID: 16694062
You should check and make sure that the number is actually being returned in the query.  Can you profile the call to the database?
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 16695185
I do have .NET 2.0 VS 2005 C#..........

The database call simply fills a dataset. Seems like a simple task, but............... I even tried changing the data type to decimal in both DB column and C# code

string sql =  " Select * from Sales ";
            cn = new SqlConnection("integrated security=SSPI;server=localhost; persist security info=False;initial catalog=SalesDb");
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            cn.Open();
            da.Fill(dataset1, "Sales");            
            cn.Close();


foreach (DataRow dr in dataset1.Tables["Sales"].Rows)
 {
      int intDay = Convert.ToInt16(dr["Day"]);
 }
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 16695213
But what I am asking is have you looked at the actual data that is being transmitted.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 16698182
the data passes through into a textbox like it should (number between 0-31), except for this minor error, the method works............
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 16700092
Wait.  THe data shows as a number in a textbox?  THen at what point are you getting this error?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

830 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