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?
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 ThaimainWindow.Close()just after you callhomeWindow.Show()and remove the call toApplication.Current.MainWindow.Close()? - mm8