By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

error Unable to cast object of type 'System.Data.DataColumn' to type 'System.IConvert

P: 11
hi friends,
i have one class and i have created two properties

and filled this property through dataset

but when i created object o this class to access this property to fill textbox

i am geting this problem

"Unable to cast object of type 'System.Data.DataColumn' to type 'System.IConvertible"

Please give me suggestion

Expand|Select|Wrap|Line Numbers
  1. public class Class1
  2. {
  3.     SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cnstr"]);
  4.     SqlDataAdapter da = new SqlDataAdapter();
  5.     SqlCommand cmd = new SqlCommand();
  6.     DataSet ds = new DataSet();
  7.     SqlDataReader dr;
  8.     public Class1()
  9.     {
  10.         //
  11.         // TODO: Add constructor logic here
  12.         //
  13.     }
  14.        public void getdata1()
  15.     {
  16.         cmd.Connection = cn;
  17.         cn.Open();
  18.         cmd.CommandType = CommandType.Text;
  19.         cmd.CommandText = "SELECT EmployeeID, FirstName FROM    employees ORDER BY FirstName";
  20.         da.SelectCommand = cmd;
  21.         da.Fill(ds);
  22.  
  23.         this._CategoryId = Convert.ToInt16(ds.Tables[0].Columns["employeeid"]);
  24.         this._firstname = ds.Tables[0].Columns["firstname"].ToString();
  25.  
  26.  
  27.  
  28.        private int _CategoryId;
  29.  
  30.     public int CategoryId
  31.     {
  32.         set { _CategoryId = value; }
  33.         get { return _CategoryId; }
  34.     }
  35.     private string _firstname;
  36.  
  37.     public string firstname
  38.     { 
  39.         set { _firstname = value; }
  40.         get { return _firstname; }
  41.  
  42.     }
  43.     private string _lastname;
  44.  
  45.     public string lastname
  46.     {   
  47.         set { _lastname = value; }
  48.         get { return _lastname; }
  49.  
  50.     }
  51.  
this is my .cs file code

Expand|Select|Wrap|Line Numbers
  1. Class1 objcls = new Class1();
  2.  this.objcls.getdata1();
  3.  
  4.         txtid.Text = objcls.CategoryId.ToString();
  5.         txtname.Text = objcls.firstname.ToString();
Dec 7 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.