Im trying to use a case statement to assign a value to a variable named "query". Depending on the value of a comboBox, the value of query will change. I assigned the "query" variable inside my method and want to use it within the method only. I get an error message that the "query" variable is unassigned even though it is being assigned at the top of the method. I have a work around but I dont know why this happens? any insight would be helpfull.
heres the code.
public void ExportKml()
{
string query;
switch (txtTable.SelectedIndex)
{
case 0:
query = "Select * from dbo.HyacinthWaterBodyZones";
break;
case 1:
query="Select * from lchcd.privateWatersFinal where waterbodypolygon is not null";
break;
case 2:
query = "Select * from lchcd.publicWatersFinal where waterbodypolygon is not null";
break;
}
cs.Open();
SqlCommand cmd = new SqlCommand(query, cs); <<--Error Message
SqlDataReader polygon = cmd.ExecuteReader();
}
the "query" variable inside the line: SqlCommand cmd = new SqlComman(query,cs) gives an error stating it is unassigned local variable.