Home > database >  How to debug sql connection establishment c# winforms
How to debug sql connection establishment c# winforms

Time:06-24

im trying to create a login form

table userlogin has id,username,passwort

var user = rjTextBox1.Text;
        var pass = rjTextBox2.Text;
        using (MySqlConnection conn = new MySqlConnection("datasource=127.0.0.1;port=3306;username=root;database=exstructa;"))
        {
            conn.Open();
            MySqlCommand command = new MySqlCommand("select benutzername, passwort from userlogin where benutzername = @Username and passwort = @Password", conn);
            command.Parameters.AddWithValue("@Username", user);
            command.Parameters.AddWithValue("@Password", pass);
            MySqlDataReader reader = command.ExecuteReader();
            if (reader.Read() == true)
            {
                Dashboard dashboardform = new Dashboard();
                dashboardform.Show();
                this.Hide();
                MessageBox.Show("Willkommen", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Falsche Zugangsdaten", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

trying to login with an identical existing user in the db still gives me an error, is there something wrong with the code or how can i debug the connection?

Thanks in advance

CodePudding user response:

Wrap your code with this:

try
{
   ...your code...
}
catch (Exception ex)
{
    MessageBox.Show($"{ex.Message}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(MySQLException sqlex)
{
   foreach(MySQLErrorCode err in sqlex.Errors)
   {
      //handle errors
      MessageBox.Show($"{err.Number}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
}

And check what errors come out.

CodePudding user response:

It threw the Exception "Not Supported" and it seems that as of today, Visual Studio 2022 doesn't support MySQL yet.

  • Related