title: "Specified cast is not valid" error in C# windows forms program
body: <p>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.</p>
  
  <p>here's the source:</p>
  
  <pre><code>using (SqlDataReader rdr = cmd.ExecuteReader()) //"select * from table where fieldname = " + value
  {
  
   while (rdr.Read())
   {
    int number = (int)rdr["quantity"]; // error is here
  </code></pre>