I am trying to create login screen with WPF, i have input username and password in SQL data base and connected with C# code. When i am designing in visual studio, i am not getting any error messages and everything seams fine, but when i run application and login screen shows up, i put username and password in to fields but i still get error message that information are incorrect but application still let me trough to next window.Bellow is my code in xaml and c#.
private void buttonLogin_Click(object sender, RoutedEventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Denis\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Login where Username='" + textBoxUsername + "' and Password='" + textBoxPassowrd + "'", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count>0)
{
string username = dataSet.Tables[0].Rows[0]["Username"].ToString();
Close();
}
else
{
MessageBox.Show("Invalid username or password!", "Paznja", MessageBoxButton.OK, MessageBoxImage.Error);
}
this.Hide();
MainWindow mn = new MainWindow();
mn.Show();
this.Close();
}