467,926 Members | 1,744 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

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

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
  • viewed: 3493
Share:

Post your reply

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

Similar topics

reply views Thread by Pankaj Jain | last post: by
4 posts views Thread by Richard Lee | last post: by
20 posts views Thread by MadCrazyNewbie | last post: by
3 posts views Thread by Jon | last post: by
reply views Thread by Wes Shaddix | last post: by
1 post views Thread by explode | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.