title: Stored procedure InvalidCastException error
body: <p>I was trying out accessing stored procedures in a simple console app and for some reason I get a <code>InvalidCastException</code>.</p>
  
  <p>Can anyone help?</p>
  
  <pre><code>    class Customer
      {
          public int CustomerID { get; set; }
          public string FirstName { get; set; }
          public string SecondName { get; set; }
          public int Age { get; set; }
      }
  
      static void Main(string[] args)
      {
          string storedProc = "dogssimple";
          List&lt;Customer&gt; custList = new List&lt;Customer&gt;();
  
          SqlConnection conn = new SqlConnection("server=localhost;" +
                                     "Trusted_Connection=yes;" +
                                     "database=mydatabase; " +
                                     "connection timeout=30");
          try
          {
              conn.Open();
  
              SqlCommand cmd = new SqlCommand(storedProc, conn);
  
              cmd.CommandType = CommandType.StoredProcedure;
  
              SqlDataReader rdr = cmd.ExecuteReader();
  
              while (rdr.Read())
              {
                  Customer addCustomer = new Customer() //..... cast exception here
                  {
                      CustomerID = (int)rdr["CustomerID"],
                      FirstName = (string)rdr["FirstName"],
                      SecondName = (string)rdr["SecondName"],
                      Age = (int)rdr["Age"],
                  }; //....
  
                  Console.WriteLine("Customer: {0}, {1}, {2}, {3}", addCustomer.CustomerID, addCustomer.FirstName, addCustomer.Age, addCustomer.Age);
                  custList.Add(addCustomer);
              }
  
              var age = from x in custList
                        select x.Age;
              var avgAge = age.Average();
  
              Console.WriteLine("The average age of the customers is " + avgAge);
          }
          catch (Exception e)
          {
              Console.WriteLine(e); ;
          }
  
          Console.ReadLine();   
  }
  </code></pre>