shanem

shanem

  • NA
  • 1
  • 0

Getting Integer Values from DataReader

Nov 17 2003 2:30 PM
How can I get C# to pull back a value from a datareader that is a datatype of int in a sql database. The following code returns an InvalidCastException error: SqlCommand cmd = new SqlCommand("SELECT name, xtype, length, colstat FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '" + Table + "')", cn); SqlDataReader dr = cmd.ExecuteReader(); SqlParameter prm; long xtype = 0; while (dr.Read()) { xtype = (long)dr.GetSqlInt64(1); } xtype in syscolumns in SQL is a datatype of tinyint. I have tried using dr.GetSqlInt16, GetSqlInt32, and GetSqlInt64 as well as throwing it into a variable declared as integer, short, and long. I've also tried using dr.GetInt16 (instead of GetSqlInt16), GetInt32, and GetInt64...same error. It seems that C# doesn't like anything except strings and objects when it comes to the datareader and I need to be able to get an integer value back. Thoughts?

Answers (1)