The code looks OK, with the exception that it only deals with two values: If the database contains 14 "Alpha" Groups and 27 "Beta" Groups, it will only show the last one of each to be read from the database. The only small change I might make is to make it case independent:
switch (currentGroupName.ToLower())
{
case "alpha":
AlphaX = reader["X"].ToString();
AlphaY = reader["Y"].ToString();
break;
case "beta":
BetaX = reader["X"].ToString();
BetaY = reader["Y"].ToString();
break;
}
What is it doing that it shouldn't, or not doing that it should?
"There are 5 groups, Alpha, Beta, Gamma etc. Uhmmm basically the switch is saying case "Alpha" then the string AlphaX should = the value "X" from the database. However it does not store it since the messagebox at the bottom of the code still says null when displayed :("
So, the first thing to do is look at the data you are getting out.
Either put a breakpoint at the
switch
statement, and look then single step, or add a line just after the Group name read:
string currentGroupName = reader["GroupName"].ToString();
Console.WriteLine(currentGroupName +
" (" +
reader["X"].ToString() +
"," +
reader["Y"].ToString() +
")");
switch (currentGroupName)
{
That will tell you what data you are getting. From that you shouldbe able to work out where the problem is.
If not, post the data and I'll have a look.
"Ok woops, my bad I accidently changed the connection feild. Yeah it actually displays fine"
Alpha (500 ,455 )
Beta (56 ,34 )
Gamma (90 ,95 )
Delta (34 ,34 )
Omega (64 ,6 )
Did you do the formatting? Or was that how it printed?
Are there spaces at the end of the Group name?
If so then "Alpha " will not match "Alpha": the strings are different lengths.
Try:
switch (currentGroupName.ToLower().Trim())
{
case "alpha":
AlphaX = reader["X"].ToString();
AlphaY = reader["Y"].ToString();
break;
case "beta":
BetaX = reader["X"].ToString();
BetaY = reader["Y"].ToString();
break;
}
Note that I used lower case throughout to eliminate that possibility as well.