Below is switch case
switch (strID)
{
case ConfigurationManager.AppSettings["Key1"].ToString():
Label1.Visible = true;
break;
case ConfigurationManager.AppSettings["Key2"].ToString():
Label2.Visible = true;
break;
case ConfigurationManager.AppSettings["Key3"].ToString():
Label3.Visible = true;
break;
default:
Label1.Visible = true;
break;
}
But it gives error "A constant value is expected."
I know that you can't have variables in the switch statement.But is any way ?
You can use only constant value in case statement.
Better you can use if statement e.g.
if(ConfigurationManager.AppSettings["Key1"].ToString() == strID)
{
Label1.Visible = true;
}
else if(ConfigurationManager.AppSettings["Key2"].ToString() == strID)
{
Label2.Visible = true;
}
. . . . . . .
else
{
//default
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With