"Specified cast is not valid." error

  • Thread starter Thread starter Curious
  • Start date Start date
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.
 
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.
 
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.
 
* 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.
 
* 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!
 
Back
Top