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

前端 未结 9 1219
南方客
南方客 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:15

    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!");
        }
    }
    

提交回复
热议问题