“Specified cast is not valid” error in C# windows forms program

前端 未结 9 1234
南方客
南方客 2020-12-10 13:20

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 numer

9条回答
  •  无人及你
    2020-12-10 14:22

    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"]);
    

提交回复
热议问题