Integer cast DataRow value???

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"]);
 }
LVL 1
kvnsdrAsked:
Who is Participating?
 
mrichmonCommented:
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
 
Fahad MukhtarDistinguished EngineerCommented:
change
intDay = Convert.ToInt16(dr["Day"]);

to
intDay = dr["Day"]==DBnull.Value?0:Convert.ToInt16(dr["Day"]);
0
 
Jase-CoderCommented:
There is no method ToInt16(); in the object class

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
aprestoCommented:
try

short.Parse(dr["Day"].ToString());
0
 
mrichmonCommented:
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
 
kvnsdrAuthor Commented:
That was my first thought, so I made sure all rows in that column had a number. However, the error persists.........
0
 
kvnsdrAuthor Commented:
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
 
mrichmonCommented:
But what I am asking is have you looked at the actual data that is being transmitted.
0
 
kvnsdrAuthor Commented:
the data passes through into a textbox like it should (number between 0-31), except for this minor error, the method works............
0
 
mrichmonCommented:
Wait.  THe data shows as a number in a textbox?  THen at what point are you getting this error?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.