Results 1 to 6 of 6

Thread: [Resolved] Application not using switch for returned string from server

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    40

    Resolved [Resolved] Application not using switch for returned string from server

    Alright, I have a program communicating with my server to login. The server sends back a success or failure code. The application gets the code, but doesn't use the switch/case I have set up in order to proceed.

    Code:
    string returnString = WebConnection.SendPostData("http://site.com/login.php", "username=" + strUsername + "&password=" + strPassword);
                //MessageBox.Show(returnString);
                switch (returnString)
                {
                    case "1":
                        {
                            //Login success
                            MessageBox.Show("Login successful.");
                            //Insert Login code here
                            break;
                        }
                    case "2":
                        {
                            //Login failure
                            MessageBox.Show("Login failed.");
                            Application.Exit();
                            break;
                        }
                }
    If I enable the messagebox to display returnString, it properly shows me either 1 or 2, which it gets from the server. However, from there it doesn't display either of the messageboxes which show success or failure or execute the code it should be using. If I set one of the cases to default, it takes that regardless of the server's response. Is there something I'm missing with the way I'm using case to compare a string?
    Last edited by Tural; Aug 9th, 2007 at 06:01 PM. Reason: Resolved.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Application not using switch for returned string from server

    Are you absolutely positive that the returned string is just single character? There are no spaces or new lines hiding in there?

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    40

    Re: Application not using switch for returned string from server

    Yep.

    PHP Code:
    if($row['password'] == $password && $row['approved'] == 1)
            {
                echo 
    "1";
            }
            else if(
    $row['password'] != $password && $row['approved'] == 1)
            {
                echo 
    "2";
            } 

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Application not using switch for returned string from server

    Quote Originally Posted by Tural
    Yep.

    PHP Code:
    if($row['password'] == $password && $row['approved'] == 1)
            {
                echo 
    "1";
            }
            else if(
    $row['password'] != $password && $row['approved'] == 1)
            {
                echo 
    "2";
            } 
    That wouldn't assure me; particularly since there is nothing wrong with your switch. Notice that if you pass it a correct string, it works just fine.

    c# Code:
    1. string returnString = "1";
    2.  
    3. switch (returnString)
    4. {
    5.     case "1":
    6.         {
    7.             //Login success
    8.             MessageBox.Show("Login successful.");
    9.             //Insert Login code here
    10.             break;
    11.         }
    12.     case "2":
    13.         {
    14.             //Login failure
    15.             MessageBox.Show("Login failed.");
    16.             Application.Exit();
    17.             break;
    18.         }
    19. }

    Actually step through your code and see the value of returnString. It's not what you think it is.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    40

    Re: Application not using switch for returned string from server

    After further messing with it, I see it is indeed returning the value with a line break at the end. I'll throw a substring in there to get rid of it. Thanks for the help.
    Last edited by Tural; Aug 9th, 2007 at 06:01 PM.

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Application not using switch for returned string from server

    You bet.

    I don't know anything about PHP, but if you are going to use the return value like this why not just return a boolean or integer instead of messing around with a string?

    Also, if you're done with this question, then you can mark the thread as Resolved.

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width