0
votes

I am very new to C# and WPF as I am learning on my own.

I have implemented a login screen and I want it act in a typical manner: User enters login informations (username, pw). If info are OK, login screen should close and next screen shows up. This is how I have done this:

My XAML code for the button

<Button x:Name="BtnHelloConnect" Content="Connect" Click="BtnHelloConnect_Click"  IsDefault="True"/>

On click, this code-behind is fired:

private void BtnHelloConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                using (var Connect = new SqlConnection(connstr))
                {
                    Connect.Open();

                    foreach (ConnectResponse connectResponse in new CheckConnection().CheckIdentity(TextBoxLoginID.Text, PasswprdBoxLoginMDP.Password, ComboBoxLoginInst.Text))
                    {
                            if (connectResponse.Reponse == "1")
                            {
                                LoggedInData.LoggedInUserId = TextBoxLoginID.Text; //These are some classes that I have created to stored logged-in Data
                                LoggedInData.LoggedInstitutionId = connectResponse.Entity;

                                AuthentificationAccess.CheckPrivilege(LoggedInData.LoggedInUserId, LoggedInData.LoggedInstitutionId);
                            }                            
                            else
                            {
                                MessageBox.Show(connectResponse.Reponse, "", MessageBoxButton.OK, MessageBoxImage.Stop);
                                return;
                            }
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


internal class CheckConnection
{
    //Here is the method where I execute a procedure to check whether the user has entered the right loggins. The method return a string "connectResponse"
}

internal class ConnectResponse
    {
        public string Reponse { get; set; }

        public string Entity { get; set; }
    }


public static class AuthentificationAccess
    {        
        public static void CheckPrivilege (string username, string entityid)
        {
            string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

            HomeWindow homeWindow = new HomeWindow();
            MainWindow mainWindow = new MainWindow();

            using (var Connect = new SqlConnection(connstr))
            {
                Connect.Open();
                using (var Command = new SqlCommand("My Procedure", Connect))
                {
                    Command.CommandType = CommandType.StoredProcedure;
                    Command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
                    Command.Parameters.Add("@entity_id", SqlDbType.VarChar).Value = entityid;
                    SqlDataReader dr = Command.ExecuteReader();
                    while (dr.Read())
                    {
                        string UserCategory = dr.GetString(0); 

                        if (UserCategory == "Client")
                        {
                            homeWindow.MenuBarProfile.Visibility = Visibility.Collapsed;
                        }
                        else
                        {
                            MessageBox.Show(UserCategory, "", MessageBoxButton.OK, MessageBoxImage.Stop);
                            mainWindow.Show();
                            return;
                        }
                    }
                    Application.Current.MainWindow.Close();
                    homeWindow.Show();                   
                }
            }
        }

    }

The problem that I have is happening while executing the closing of the main window (the login window) with the command: Application.Current.MainWindow.Close();.

The first time I login, everything works fine: the main window closes and the second window opens.

But the second time I login, the mainindow does not close while the second window opens.

I have spent 3 days trying to find a solution for this and me just learning out of blogs and Youtube videos, I haven't been able to solve this.

I know there are a lot of similar questions here with the same issues, but most deal with solutions in MVVM. I am not really familiar with everything MVVM so I am findint it hard to reproduce. Given my implementation, is there an easy way to solve this?

2
Try this.Close(); inside of the window you want to close - VollRahm
question: depending on the user, you show HomeWindow or MainWindow, right? - Marlonchosky
This.Close() was the first approach I took but it is not recognised in the method AuthentificationAccess. No, I only show HomeWindow if the login is successful. If the login fails, the user stays at MainWindow. If the user logs out, he comes back to the mainWindow also (which is the login screen). - Bruno Bukavu Thai
@BrunoBukavuThai: Call mainWindow.Close() just after you call homeWindow.Show() and remove the call to Application.Current.MainWindow.Close()? - mm8

2 Answers

1
votes

I don't think CheckPrivilege is the "right" place to be calling Close. Instead I think you should change the method to be:

public static string CheckPrivilege (string username, string entityid)

It would return the "User Category" if the credentials were valid, otherwise null. And it wouldn't do anything about any windows. That gives you a nice static method that you could call anywhere in your app to check credentials.

The specific UI logic of closing the login window and opening the main one, along with hiding/showing various elements, would be handled inside the login window or the other windows involved by using that return value. This way you could call this.Close() from the button click.

0
votes

Instead of using two windows, you can use one window with 2 different pages. if the user is authenticated, you can easily navigate to the next page.