0

I want to create switch cases using dynamic values from listbox which is getting values from Sql Database but getting an error that is "A constant value is expected" how to use switch statement then to avoid this error. here's the code

    con.Open();
    SqlCommand cmd = new SqlCommand("select UserName from Receptionist", con);
    SqlDataReader dr = cmd.ExecuteReader();
    while(dr.Read())
    {
    listBox1.Items.Add(dr.GetString(dr.GetOrdinal("UserName")));
    }
    dr.Close();
    con.Close();

here I am accessing the listbox text.

    case listBox1.Text:
    txtUserName.Text = speech;
    break; 

case listBox1.Text is the text where the error generated. Thanks in advance.

  • 1
    Does this answer your question? [Switch case in C# - a constant value is expected](https://stackoverflow.com/questions/7593377/switch-case-in-c-sharp-a-constant-value-is-expected) – Crowcoder Jun 26 '21 at 11:32

0 Answers0