"Specified cast is not valid." error


C

Curious

I got runtime error when the ran the following:

object tshares = mReader["target_qty"];
int targetShares = (int)tshares;

Why can't I cast object to int? The value is shown in database as
2.0000. I thought it was double. However, if I cast it to double, I
would get the same error. Any advice on how to get this fixed? Thanks.
 
Ad

Advertisements

C

Curious123

Curious said:
I got runtime error when the ran the following:
                    object tshares = mReader["target_qty"];
                    int targetShares = (int)tshares;
Why can't I cast object to int? The value is shown in database as
2.0000. I thought it was double. However, if I cast it to double, I
would get the same error. Any advice on how to get this fixed? Thanks.

Wny don't you just do this?

int targetShares = ConvertToInt32(mReader["target_qty"]);

What's the namespace of "ConvertToInt32"? It gives me an error, the
name "ConvertToInt32" does not exist in the current context.
 
F

Family Tree Mike

Curious123 said:
Curious said:
I got runtime error when the ran the following:
object tshares = mReader["target_qty"];
int targetShares = (int)tshares;
Why can't I cast object to int? The value is shown in database as
2.0000. I thought it was double. However, if I cast it to double, I
would get the same error. Any advice on how to get this fixed? Thanks.
Wny don't you just do this?

int targetShares = ConvertToInt32(mReader["target_qty"]);

What's the namespace of "ConvertToInt32"? It gives me an error, the
name "ConvertToInt32" does not exist in the current context.

He meant Convert.ToInt32(mReader["target_qty"]). The Convert class is
in the System namespace.
 
J

Jesse Houwing

* Mr. Arnold wrote, On 14-10-2009 17:28:
Curious said:
I got runtime error when the ran the following:

object tshares = mReader["target_qty"];
int targetShares = (int)tshares;

Why can't I cast object to int? The value is shown in database as
2.0000. I thought it was double. However, if I cast it to double, I
would get the same error. Any advice on how to get this fixed? Thanks.


Wny don't you just do this?

int targetShares = ConvertToInt32(mReader["target_qty"]);

That would probably work, but doesn't really solve the issue. My guess
is that it is probably a decimal, as both a cast to double and int fail.

Best way to check this is to place a breakpoint on the line.
Run the application. And when the breakpoint is hit, use quickwatch on
mReader["target_qty"]. That should show both the value, but also the
actual type in the Quickwatch window.

Then stop debugging and fix the code.
 
Ad

Advertisements

C

Curious

* Mr. Arnold wrote, On 14-10-2009 17:28:
Curious said:
I got runtime error when the ran the following:
object tshares = mReader["target_qty"];
int targetShares = (int)tshares;
Why can't I cast object to int? The value is shown in database as
2.0000. I thought it was double. However, if I cast it to double, I
would get the same error. Any advice on how to get this fixed? Thanks.
Wny don't you just do this?
int targetShares = ConvertToInt32(mReader["target_qty"]);

That would probably work, but doesn't really solve the issue. My guess
is that it is probably a decimal, as both a cast to double and int fail.

Best way to check this is to place a breakpoint on the line.
Run the application. And when the breakpoint is hit, use quickwatch on
mReader["target_qty"]. That should show both the value, but also the
actual type in the Quickwatch window.

Then stop debugging and fix the code.

Yes, it's decimal. I saw that in the debugger. I cast it to decimal at
first. Then cast it to int. Thanks to all for your inputs!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top