Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem where its not storing the variable properly. I'm not great with switch statements but belive the logic is right since this code was suggested before by someone else. If anyone can tell me why the variables are not being stored I would be very greatfull.

Code :

string AlphaX = null, AlphaY = null, BetaX = null, BetaY = null;                

                SqlCommand userCommand = userConnection.CreateCommand();

                userCommand.CommandText = "SELECT X, Y, GroupName FROM [dbo].[Coordinates]";

                SqlDataReader reader = userCommand.ExecuteReader();

                while (reader.Read())
                {
                    string currentGroupName = reader["GroupName"].ToString();

                    switch (currentGroupName)
                    {
                        case "Alpha":
                            AlphaX = reader["X"].ToString();
                            AlphaY = reader["Y"].ToString();
                            break;
                        case "Beta":
                            BetaX = reader["X"].ToString();
                            BetaY = reader["Y"].ToString();
                            break;
                    }
                 }
                 MessageBox.Show(AlphaX); //Test the variables value.



Update---

If I can't do it this way, is there a way of me asking for multiple commands from the database for each of the 5 rows?
Posted
Updated 14-Mar-11 8:23am
v2

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:
C#
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:
C#
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"

VB
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:
C#
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.
 
Share this answer
 
v3
Comments
WurmInfinity 14-Mar-11 11:48am    
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 :(
OriginalGriff 14-Mar-11 12:07pm    
Answer updated
WurmInfinity 14-Mar-11 12:16pm    
Well when I run Console.WriteLine(currentGroupName + " (" + reader["X"].ToString() + "," + reader["Y"].ToString() + ")"); All the data thats in the database comes up properly. This is testing it in console, since my main program is windows form. So the connection to the database is fine, its just not storing the value thats in the X column into my variable AlphaX in the switch :S
OriginalGriff 14-Mar-11 12:22pm    
It doesn't matter if it's console or Winforms: Console.WriteLine still works.
In the debugger for Winforms, it is printed in the Output pane of the panel which holds compilation error messages and so forth. Try it in situ: there may be a difference you don't know about!
WurmInfinity 14-Mar-11 12:30pm    
Sorry for being stupid, but im in visual studio and I do not see a console pane or any output pain that displays anything when im debugging. Is it under a menu im looking over?
Did you try to debug the code?
I would first check if the query returns the expected results. I really have my doubts on it.
 
Share this answer
 
Comments
WurmInfinity 14-Mar-11 11:18am    
Yeah I have debugged it, the code compiles aswell and when you run it the MessageBox just comes up empty and appears like 4 times which I dont get either :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900