Hi, before inserting it into the database you need to check whether the username is already available in the database or not. You can try the below code.
if (txtUser.Text != "")
{
conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
conn.Open();
string username = txtUser.Text;
command.CommandType = CommandType.Text;
command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
command.CommandText = "SELECT * FROM tblUser WHERE Username = @username";
command.Connection = conn;
SqlDataReader reader = null;
conn.Open();
reader = command.ExecuteReader();
if (reader.HasRows)
{
MessageBox.Show("The username is already available, type a new one");
reader.Close();
}
else{
command.CommandText = "INSERT INTO tblUser(Username) values ('" + txtUser.Text + "')";
command.ExecuteNonQuery();
conn.Close();
this.Hide();
Home home = new Home();
home.Show();
}
}
else
{
lblValidation.Visible = true;
txtUser.Focus();
}
conn.Close();
}