Search code examples
c#silverlightwindows-phone-8

Multiple conditions in Windows Phone Silverlight development


I am beginning Windows Phone development with Silverlight and web services. I have a web services for User Login where I have two checks before the user is Allowed to log in. I am unable to perform both checks. Only one check is called. My code:

private void login_action(object sender, RoutedEventArgs e)
        {
            string _username = txtUser.Text;
            string _password = txtPass.Password;
            kollserviceClient client = new kollserviceClient(); 
            client.validUserCredentialAsync(_username, _password);
            client.validUserCredentialCompleted += Client_validUserCredentialCompleted;
            client.isStudentUserAsync(_username);
            client.isStudentUserCompleted += Client_isStudentUserCompleted;

        }

        private void Client_isStudentUserCompleted(object sender, isStudentUserCompletedEventArgs e)
        {
            if (!e.Result)
            {
                MessageBox.Show("User is Not a Student. Unable to Login", "Error", MessageBoxButton.OK);
                return;
            }
        }

        private void Client_validUserCredentialCompleted(object sender, validUserCredentialCompletedEventArgs e)
        {
            if (e.Result)
            {
                IsolatedStorageSettings.ApplicationSettings["lgusername"] = txtUser.Text;
                NavigationService.Navigate(new Uri("/Home.xaml", UriKind.RelativeOrAbsolute));
            }
        }

If the Credentials are valid the user is able to log in whether he/she is a student user or not. How can I make both check to be executed?


Solution

  • The way this is coded, I don't believe that you can guarantee the order in which the service calls will return. Therefore, you could store the results from each call, then call a 3rd method that evaluates that both calls have returned. Another option would be to chain the calls so that it does not check if the user is a student until it returns from the credential check and passes, then you can navigate from the return of that call.

    First option example:

    private void login_action(object sender, RoutedEventArgs e)
    {
        string _username = txtUser.Text;
        string _password = txtPass.Password;
        kollserviceClient client = new kollserviceClient(); 
        client.validUserCredentialAsync(_username, _password);
        client.validUserCredentialCompleted += Client_validUserCredentialCompleted;
        client.isStudentUserAsync(_username);
        client.isStudentUserCompleted += Client_isStudentUserCompleted;
    
    }
    private bool? isStudent = null;
    private bool? isAuthenticated = null;
    
    private void Client_isStudentUserCompleted(object sender, isStudentUserCompletedEventArgs e)
    {
        isStudent = e.Result;
        EvaluateAndNavigate();
    
    }
    
    private void Client_validUserCredentialCompleted(object sender, validUserCredentialCompletedEventArgs e)
    {
        isAuthenticated = e.Result;
        if (isAuthenticated)
        {
            IsolatedStorageSettings.ApplicationSettings["lgusername"] = txtUser.Text;
        }
        EvaluateAndNavigate();
    }
    
    private void EvaluateAndNavigate()
    {
        if(isStudent.HasValue && isAuthenticated.HasValue) //both calls have returned
        {
            if(isStudent.Value && isAuthenticated.Value)
            {
                NavigationService.Navigate(new Uri("/Home.xaml", UriKind.RelativeOrAbsolute));
            }
            else
            {
                MessageBox.Show(string.Format("{0}Unable to Login", isStudent.Value ? "" : "User is Not a Student. " ), "Error", MessageBoxButton.OK);
            }
        }
    }