tags:

views:

2950

answers:

8

I'm having a "Specified cast is not valid" error. Windows form application in C#. I'm trying to retrieve a value from a table. The value is either a smallint, or a numeric(i tried both fields, both give me the same error), and I try to store it in an int variable.

here's the source:

using (SqlDataReader rdr = cmd.ExecuteReader()) //"select * from table where fieldname = " + value
{

 while (rdr.Read())
 {
  int number = (int)rdr["quantity"]; // error is here
A: 

Try one of the two options.

int number = rdr.getInt32(rdr.GetOrdinal("quantity"));

or

int number = int.parse(rdr["quantity"].ToString());
Mitchel Sellers
The first option is not a valid overload, the second won't compile.
statenjason
Oops, fixed my topos, was writing from memory
Mitchel Sellers
yeah the edited second answer is right. thx
jello
+3  A: 
  if(rdr["quantity"].GetType() != typeof(int))
    throw new InvalidOperationException(
      "quantity is a " + rdr["quantity"].GetType());
  int number = (int)rdr["quantity"]; // error is here
Will
I don't see how this assigns the quantity in number variable. logically, I should still get an error, if i'm right
jello
@jello It'll throw an exception that tells you excatly what type rdr["quantity"] is. Without knowing this, all these answers are pissing in the wind. For all we know "quantity" is a friggen byte array. Put it in your code and run it.
Will
BTW, when I say "run it" I don't mean run it in release mode. Run it in debug mode from visual studio.
Will
i see. well, in this case, I created the database, so I know what datatype the fields are
jello
@jello yes, you may know the data types in the database, but you *obviously* don't know what the .NET equivalent type is, or its DBNULL, hence the "specified cast is invalid" message.
Will
A: 

Have you tried int number=convert.toint16(rdr["quantity"]);

David
should work too. thx
jello
+1  A: 

rdr["quantity"] is going to be a boxed something. If it is not an int then you can not unbox it directly to an int (which is what you are trying to do) as you have to first unbox it to the appropriate type (say, short). But this is too cumbersome so for clarity you're better off saying

Convert.ToInt32(rdr["quantity"]);
Jason
this one works. thx
jello
+1  A: 

I bet quantity is NULL, which is not an integer.

John Saunders
no it's not null. thx anyway
jello
A: 

Try using SqlDataReader's GetInt32()

rdr.GetInt32(rdr.GetOrdinal("quantity"));
statenjason
i tried that one. doesn't work. thx anyway
jello
A: 

Silly suggestion, maybe - but have you considered trying this - grab the result from your SqlDataReader as an instance of object and then checking what type it is? No one can tell you better what it really is than the CLR type system! :-)

using (SqlDataReader rdr = cmd.ExecuteReader()) 
{
    while (rdr.Read())
    {
        object obj = rdr["quantity"];

        if(obj != null)
        {
            string objType = obj.GetType().FullName;
        }
    }
 }

If you do get a value back, you can check what type it is and hopefully convert it accordingly, depending on your results.

marc_s
A: 

Since you said that you know what the value should be because you created the database... Can you check that rdr["quantity"] has a value then run a try parse on it?

if(rdr["quantity"] != null) {
    int? number = null;
    if(int.TryParse(rdr["quantity"].ToString(), out number)) {
        Console.WriteLine("Hurray, I have an int. Up vote Coov!");
    }
}
Coov