Skip to content
April 2, 2008 / kiranpatils

tinyint+invalid cast exception with DataReader

today i had faced a wired error in my DAL it was throwing an error invalid cast my code is like this:

int empid = reader.GetInt32(“EMPID”); //throws invalid cast..

i checked it with my database schema EMPID is tinyint. which is main cause of an error..

Solution:

I had done R&D and come in to know that:

SQL SERVER Stored tinyint as 8bit[1byte] .so we can’t fetch it using getint32…so to fetch it i had changed my code to:

 int empid = Convert.ToInt32(reader.GetBytes(“EMPID”)); //worked 

One Comment

Leave a Comment
  1. Nishil / Aug 9 2011 1:14 pm

    This the correct Code.
    int empid = Convert.ToInt32(reader.GetByte(“EMPID”));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: