'How can i show my main WPF window after successful login?
I started learning WPF recently and I am trying to write a simple login system.
I got it to "work", it is verifing if the credentials are correct according to the database i made and returning successful, but i couldn't make it open the main window after the login.
Can anyone help?
This is the login click event
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
if (con.State == System.Data.ConnectionState.Open)
{
con.Close();
}
if(VerifyUser(textBoxUser.Text, textBoxPassword.Password))
{
MessageBox.Show("Login Succeded", "Login", MessageBoxButton.OK, MessageBoxImage.Information);
this.Close();
}
else
{
MessageBox.Show("User or Password invalid", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
And this is the verify user code
private bool VerifyUser(string username, string password)
{
con.Open();
com.Connection = con;
com.CommandText = "select Usuario_Cod from tblUsuario where Usuario_Nome='"+username+"' and Usuario_Senha='"+password+"'";
dr = com.ExecuteReader();
if (dr.Read())
{
if(Convert.ToBoolean(dr["Usuario_Cod"]) == true)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
My exact problem is that i don't know what kind of code i have to add to make it show my main window after the login window is closed in case of success
Example: user should only get access to the main window if he provides a valid username and password
Solution 1:[1]
I got it to work adding the following:
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
if (con.State == System.Data.ConnectionState.Open)
{
con.Close();
}
if(VerifyUser(textBoxUser.Text, textBoxPassword.Password))
{
MessageBox.Show("Login Succeded", "Login", MessageBoxButton.OK, MessageBoxImage.Information);
Sistema sistema = new Sistema();
sistema.Show();
this.Close();
}
else
{
MessageBox.Show("User or Password invalid", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GTdotdev |
