Reputation: 55
I am retrieving Control cannot fall through from one case labe (case "Juventus":') to another. Any idea why? I am trying to open a different form to print based on the result of the query. Thanks
using (SqlConnection connection = new SqlConnection("Data Source=xxxx;Initial Catalog=Marketing;Integrated Security=True"))
{
SqlCommand command = new SqlCommand("select [Team] from baf where id=(select max(id) from baf)", connection);
connection.Open();
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
String AAA = (read["Team"].ToString());
switch (AAA)
{
case "Arsenal":
Print dd = new Print();
dd.Show();
break;
case "Juventus":
Print2 dd2 = new Print2();
dd2.Show();
case "Porto":
Print3 dd3 = new Print3();
dd3.Show();
break;
case "Bayern Munich":
Print4 dd4 = new Print4();
dd4.Show();
break;
}
this.Close();
}
}
}
Upvotes: 0
Views: 2385
Reputation: 43886
Unlike C or C++, C# does not allow switch/case
statements where the control flow can "fall through" from one case
into the next.
In c, this would work
switch(i)
{
case 1: printf("1");
case 2: printf("1 or 2"); break;
}
and it would print 1
and 1 or 2
if i
was 1, but only 1 or 2
if i
was 2.
In c#, this is not allowed. Every non-empty case
must exit the whole switch
statement. This can either be done by
break
return
throw
or evengoto
So you're missing the break
statement at your case "Juventus"
:
case "Juventus":
Print2 dd2 = new Print2();
dd2.Show();
break; // you forgot that
Edit: as others mentioned, empty case statements are allowed to fall through:
switch(i)
{
case 1:
case 2:
// do something if i is 1 or 2
break;
}
Upvotes: 4