I have a form in my windows form application that is for admin users so that they can add,edit and delete users. The problem i have is that admin can insert a new user which has the same username as another user which results in both users having duplicate usernames when logging into the system.
how can i prevent admin from inserting an already taken username when creating new accounts in the database.
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Data.mdf;Integrated Security=True");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("INSERT INTO Login (FirstName,Role,Username,Password,Surname) VALUES ('" + txtfirstname2.Text + "','" + rolecombo.Text + "','" + txtusername2.Text + "','" + txtpassword2.Text + "','" + txtsurname.Text + "')", con);
sda.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("SAVED SUCCESSFULLY !!!!!");
What I have tried:
I have looked across many articles which suggested adding an identity to usernames to make them unique, since i already have an ID column in the table which has an identity, i can not use the solution so therefore I need a simpler solution such as the system throwing a messagebox if username is duplicate but i'm not sure how to do that.