0

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

Pradeep
  • 27
  • 5
  • 1
    This might help : https://stackoverflow.com/questions/40636620/sqldataadapter-filldatagridview-datasource-duplicates-all-rows – anoop Sep 06 '22 at 06:14
  • 1
    either make the username unique or check wether it already exists befor you add it? – Piglet Sep 06 '22 at 07:01

1 Answers1

2

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:

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108