Reputation: 43
I have a problem. In my code I have an enum, that lists different sectors of industry. The value of the enum is read from a database.
When i try to cast the string from the database I get an InvalidCastException
. I know that the problem is the enum, since all other values that are returned are strings. How can I fix this problem.
Here is the code:
public enum EnBranche
{
Metall = 1,
Informatik = 2,
Einzelhandel = 3,
Landwirtschaft = 4,
Energie = 5,
Gesundheitswirtschaft = 6,
Industrie = 7,
Tourismus = 8,
Logistik = 9
};
var firma = FirmaFuellen(dataSet.Tables["Firmen"].Rows[i].ItemArray[1].ToString(),
dataSet.Tables["Firmen"].Rows[i].ItemArray[2].ToString(),
dataSet.Tables["Firmen"].Rows[i].ItemArray[3].ToString(),
(int) dataSet.Tables["Firmen"].Rows[i].ItemArray[4],
dataSet.Tables["Firmen"].Rows[i].ItemArray[5].ToString(),
**(EnBranche) dataSet.Tables["Firmen"].Rows[i].ItemArray[5]);**
Upvotes: 1
Views: 119
Reputation: 6374
Please try using Enum.Parse:
(EnBranche) Enum.Parse(typeof(EnBranche),
dataSet.Tables["Firmen"].Rows[i].ItemArray[5].ToString())
Upvotes: 2