Home > database >  Double data entry in C# UI and MYSQL DB
Double data entry in C# UI and MYSQL DB

Time:09-07

I'm create a new signup page successfully. whenever I enter a username and password and hit a submit button it save's the credentials but the problem here is two times entered that data.

private void sbmtbtn_Click(object sender, EventArgs e)
    {
        if (usrnmtxtbx.Text != "")
        {
            if (pswdtxtbx.Text != "")
            {
                if (cnfrmtxtbx.Text != "")
                {
                    if (pswdtxtbx.Text == cnfrmtxtbx.Text)
                    {
                        MySqlConnection Con = new MySqlConnection(ConnectionString);
                        MySqlCommand Cmd;
                        Con.Open();
                        try
                        {
                            Cmd = Con.CreateCommand();
                            Cmd.CommandText = "INSERT IGNORE INTO signup(username, password,confirm) VALUES(@username,@password,@confirm)";
                            Cmd.Parameters.Add("@username", MySqlDbType.VarChar).Value = usrnmtxtbx.Text;
                            Cmd.Parameters.Add("@password", MySqlDbType.VarChar).Value = pswdtxtbx.Text;
                            Cmd.Parameters.Add("@confirm", MySqlDbType.VarChar).Value = cnfrmtxtbx.Text;

                            DataTable table = new DataTable();
                            MySqlDataAdapter adapter = new MySqlDataAdapter();
                            adapter.SelectCommand = Cmd;
                            adapter.Fill(table);
                            Cmd.ExecuteNonQuery();
                            MessageBox.Show("Your Account resgistred Successfully", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        finally
                        {
                            Con.Close();
                        }
                    }
               } 
          }
     }
 }

OutPut :

idsignup username password confirm

1 hello 123 123

2 hello 123 123

CodePudding user response:

Cmd.ExecuteNonQuery(); inserts the row into the table; that's all the code you need. The DataTable and MySqlDataAdapter are completely unnecessary (and duplicating the data); you should delete that code.

Also, storing users' passwords directly in your database is an extremely bad idea and should be completely avoided. Please research "password hashing" and "salting" and store the passwords securely. Ideally you could use an existing ASP.NET identity system and avoid building your own.

Some existing answers:

  • Related