0

I am trying to validate user data and when it is correct navigate to another xaml page. what I have, it and show the MainPage which is the Login window but when I enter data and click the button it closes the window, it's not checking the user input.

Here is what I have:

        private void Login_Click(object sender, RoutedEventArgs e)
       {
            Connection.Open();
            // sda = new MySqlDataAdapter("select count(*) from customers where Name = '" + loginName.Text + "' and Password = " + password.Password +"", Connection);
            string query = "SELECT * FROM customers WHERE Name = '" + loginName.Text + "' and Password = '" + password.Password + "'";
            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = query;
            MySqlDataAdapter sda = new MySqlDataAdapter(query, Connection);
            DataTable dt = new DataTable();
            sda.Fill(dt);
           


            if (dt.Rows.Count == 1)
            {
                this.Frame.Navigate(typeof(BlankPage1));

                /* BlankPage1 main = new BlankPage1();
                main.Show();
                this.Hide();
                Connection.Close(); */
            }
            else
            {
                message.Text = "Wrong Name or Password! Please, try again!";
                /* var messageDialog = new MessageDialog("Wrong Name or Password");
                await messageDialog.ShowAsync(); */
            } 
            Connection.Close(); 
       }

The commented statements are different things that I was trying.

Please, I need help to find out the problem, I will really appreciate any help.

Thank you in advance!

L3ly
  • 35
  • 1
  • 5
  • What do you mean "It closes the window"? Is it crashing? If yes, do you have any error log to give? Moreover, be careful with using user input directly in SQL queries. Check SQL injection. – T.Trassoudaine Dec 09 '21 at 13:40
  • It doesn't show any highlighted statements or messages indicating errors, which makes it more difficult to see what is wrong. Like I say the Login Window run but once I click the button it doesn't do anything. I have my Database connected to Visual Studio. – L3ly Dec 09 '21 at 13:45
  • "Doesn't do anything" is very different from "closes". I am not talking about compilation errors, I am talking about runtime error logs. Are you even sure the given function is executed at all? – T.Trassoudaine Dec 09 '21 at 14:01
  • Sorry, I am kinda new in this and I still learning the correct vocabulary. No errors at all. The window popups, I enter a name and password to test it and when I click the button, the window just disappears. – L3ly Dec 09 '21 at 14:14
  • Ok I think we need more information about the context of this function, where it is executed ; Make sure it is executed (Put a log in it). What do you use to create your PopUp? Is this a normal behaviour that it is closing when you click the Login button? Update your question with the added information. – T.Trassoudaine Dec 09 '21 at 14:37
  • Put this method in a try/catch and then put a breakpoint in the catch. You instantiate a `MySqlCommand` but you don't assign the `Connection` property so it looks like it's throwing an exception because `Connection` is not set. I'm pretty sure you'll see an exception telling you as much. – madreflection Dec 09 '21 at 16:15
  • 1
    Two review notes: 1) make sure you use a `using` block on your `MySqlCommand` instance so it gets disposed at the end, and 2) use `connection.CreateCommand()` instead of `new MySqlCommand()` so it already has the connection associated with it. – madreflection Dec 09 '21 at 16:17
  • @madreflection If you look closely, `cmd` is instantiated but not used. If this is throwing an exception, the author should see it in the error logs right? But, authors said "No errors at all". – T.Trassoudaine Dec 09 '21 at 16:58
  • 1
    @T.Trassoudaine: Wow. Yep, totally missed that. But the suggestion may still be helpful. OP commented that they're new, so they may not know how to get to the error log (and we've heard nothing back about it). My suggestion gives direct access to the exception. – madreflection Dec 09 '21 at 17:03
  • Hello, I have this: MySqlConnection Connection = new MySqlConnection("server=localhost; user id=root; database=atm"); just before the method I posted. to create the wrong message I tried the two options on the else statement but none is working, neither the validation of user name and password The idea is when the user clicks the login btn and the name and password are correct popups the new frame or xaml file where the user will do some ATM functions, like deposit, withdrawal and transfer money. – L3ly Dec 10 '21 at 00:19
  • You need set debug point in Login_Click method. I afraid you used light dismiss control to render login page, and it was dismissed by click button without checking user data. – Nico Zhu Dec 10 '21 at 03:37

0 Answers0