|
-
Nov 17th, 2003, 02:32 PM
#1
Thread Starter
Registered User
Getting Integer values from a Datareader
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|