Skip to content

Casting enum from array from dataset

Joe

Asked Oct 26, 2006, 9:13 PM EDT2 answers
Share
LinkedInX
i am reading a single record, and initiating a new ogject with values from that record. Code is as follows: MySqlConnection conn = new MySqlConnection(connStr); //open the connection conn.Open(); string sql = pobj.SelectAdapter() + " WHERE userid = '" + puserid + "'"; MySqlDataAdapter da = new MySqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, ptab); object[] Array = ds.Tables[0].Rows[0].ItemArray; Employee emp = new Employee((Enums.enuEmployeeType)(int)Array[14], (string)Array[1], (string)Array[15], (string)Array[0], (string)Array[3], (string)Array[4], (uint)Array[10], (string)Array[6], Enums.enuStates)(int)Array[7], (uint)Array[8], (Enums.enuPrefix)(int)Array[2], (string)Array[5], (Enums.enuCountry)(int)Array[9], (Enums.enuPhoneType)(int)Array[12], (uint)Array[11], (string)Array[13]); //close the connection conn.Close(); There is now error until the code is ran, then i get an "An exception of type 'System.InvalidCastException' occurred in myapp.DLL but was not handled in user code" here is my employee constructor: public Employee(Enums.enuEmployeeType penuEmplType, string pstrPassword, string pstrUserID, string pstrGUID, string pstrFirstName, string pstrLastName,uint pintPhone1No, string pstrCity, Enums.enuStates penuState, uint pintZip, Enums.enuPrefix penuPrefix, string pstrAddress1, Enums.enuCountry penuCountry, Enums.enuPhoneType penuPhoneType, uint pintPhone1Ext, string pstrNote) : base(pstrGUID, pstrFirstName, pstrLastName, pintPhone1No, pstrCity, penuState, pintZip, penuPrefix, pstrAddress1, penuCountry, penuPhoneType, pintPhone1Ext, pstrNote) { this.strTable = "EMPLOYEE"; this.enuEmplType = penuEmplType; this.strPassword = pstrPassword; this.strUserID = pstrUserID; } I now my array is working by checking during a step through, but my casting is off somehow. I tried replacing int with uint eg. "(Enums.enuEmployeeType)(uint)Array[14]", but no luck. can antbody please help?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    does this look better? i am reading a single record, and initiating a new ogject with values from that record. Code is as follows: MySqlConnection conn = new MySqlConnection(connStr); conn.Open(); string sql = pobj.SelectAdapter() + " WHERE userid = '" + puserid + "'"; MySqlDataAdapter da = new MySqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, ptab); object[] Array = ds.Tables[0].Rows[0].ItemArray; Employee emp = new Employee((Enums.enuEmployeeType)(int)Array[14], (string)Array[1], (string)Array[15], (string)Array[0], (string)Array[3], (string)Array[4], (uint)Array[10], (string)Array[6], Enums.enuStates)(int)Array[7], (uint)Array[8], (Enums.enuPrefix)(int)Array[2], (string)Array[5], (Enums.enuCountry)(int)Array[9], (Enums.enuPhoneType)(int)Array[12], (uint)Array[11], (string)Array[13]); conn.Close(); There is now error until the code is ran, then i get an "An exception of type 'System.InvalidCastException' occurred in myapp.DLL but was not handled in user code" here is my employee constructor: public Employee(Enums.enuEmployeeType penuEmplType, string pstrPassword, string pstrUserID, string pstrGUID, string pstrFirstName, string pstrLastName,uint pintPhone1No, string pstrCity, Enums.enuStates penuState, uint pintZip, Enums.enuPrefix penuPrefix, string pstrAddress1, Enums.enuCountry penuCountry, Enums.enuPhoneType penuPhoneType, uint pintPhone1Ext, string pstrNote) : base(pstrGUID, pstrFirstName, pstrLastName, pintPhone1No, pstrCity, penuState, pintZip, penuPrefix, pstrAddress1, penuCountry, penuPhoneType, pintPhone1Ext, pstrNote) { this.strTable = "EMPLOYEE"; this.enuEmplType = penuEmplType; this.strPassword = pstrPassword; this.strUserID = pstrUserID; } I now my array is working by checking during a step through, but my casting is off somehow. I tried replacing int with uint eg. "(Enums.enuEmployeeType)(uint)Array[14]", but no luck. can antbody please help? - Joe C.
    0
  • 1 short reply
    • 0
      sorry the post is a bit messy
      0