471,325 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Unable to Cast

Hi,

I am having problem with the following code snippet:
DataTable dt = getDataFromXLS(@"c:\test\Customers.xls"); //getDataFromXLS
function reads an Excel file and return a sheet as a datatable

for (int i=0;i<dt.Rows.Count;i++)
{
DataRow dr=dt.Rows[i];
int intCustomerID = (int) dr[0]; //This line fails with error: An
unhandled exception of type 'System.InvalidCastException' occurred

//some other codes go here

}

I looked at the Excel file, the data on the first column are all integers.
But when I tried to debug my application, dr[0].GetType returns
System.Double.

How do I cast it to int?

TIA
Jan 20 '06 #1
4 4800
System.Convert.ToInt32(dr[0]);

"Danny Ni" <dn**@yahoo.com> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
Hi,

I am having problem with the following code snippet:
DataTable dt = getDataFromXLS(@"c:\test\Customers.xls"); //getDataFromXLS
function reads an Excel file and return a sheet as a datatable

for (int i=0;i<dt.Rows.Count;i++)
{
DataRow dr=dt.Rows[i];
int intCustomerID = (int) dr[0]; //This line fails with error: An
unhandled exception of type 'System.InvalidCastException' occurred

//some other codes go here

}

I looked at the Excel file, the data on the first column are all
integers. But when I tried to debug my application, dr[0].GetType returns
System.Double.

How do I cast it to int?

TIA

Jan 20 '06 #2
Hi,

I looked at the Excel file, the data on the first column are all
integers. But when I tried to debug my application, dr[0].GetType returns
System.Double.

How do I cast it to int?


There is no way to cast it, as there is no implicit cast from double to int,
you have to use Convert.ToInt32

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 20 '06 #3
int.Parse(dr[0].ToString());

but you can also use UInt16, UInt32, etc to do it as well

Danny Ni wrote:
Hi,

I am having problem with the following code snippet:
DataTable dt = getDataFromXLS(@"c:\test\Customers.xls"); //getDataFromXLS
function reads an Excel file and return a sheet as a datatable

for (int i=0;i<dt.Rows.Count;i++)
{
DataRow dr=dt.Rows[i];
int intCustomerID = (int) dr[0]; //This line fails with error: An
unhandled exception of type 'System.InvalidCastException' occurred

//some other codes go here

}

I looked at the Excel file, the data on the first column are all integers.
But when I tried to debug my application, dr[0].GetType returns
System.Double.

How do I cast it to int?

TIA


Jan 20 '06 #4
<kr***********@gmail.com> wrote:
int.Parse(dr[0].ToString());

but you can also use UInt16, UInt32, etc to do it as well


Going through a string value is a pretty inefficient way of working,
and it doesn't show what's going on.

I'd prefer (int)(double)dr[0] to show that it's actually returning a
double, but that we want it as an int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pankaj Jain | last post: by
reply views Thread by hlyall1189 | last post: by
4 posts views Thread by =?Utf-8?B?TWF0dCBMb3Zl?= | last post: by
1 post views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
1 post views Thread by =?Utf-8?B?U2NvdHQ=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.