title: How to stop adding duplicate username into database table?
body: <p>I'm working with ASP.Net web application project . in my Registration form the <code>Username</code> and the <code>Email</code> will be check if it's exist in the database or not. but my problem is if the <code>username</code> and the <code>Email</code> are exist the user can register normally and his data will be added in the database! how i can stop it from adding these data and forced the user to change the <code>username</code> or the <code>Email</code> if one of them is exist ! please any help ?</p>
  
  <p>my <code>.aspx.cs</code> page : </p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
          {
              byte[] License;
              Stream s = FileUpload1.PostedFile.InputStream;
              BinaryReader br = new BinaryReader(s);
              License = br.ReadBytes((Int32)s.Length);
                  try
                  {
                      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                      conn.Open();
                      string insertQuery = "insert into DeliveryMen (Name,Username,Password,Email,Phone,City,License) values (@name ,@username, @password, @email ,@phone ,@city,@License)";
                      SqlCommand com = new SqlCommand(insertQuery, conn);
                      com.Parameters.AddWithValue("@name", TextBoxName.Text);
                      com.Parameters.AddWithValue("@username", TextBoxUsername.Text);
                      com.Parameters.AddWithValue("@password", TextBoxPassword.Text);
                      com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
                      com.Parameters.AddWithValue("@phone", TextBoxPhone.Text);
                      com.Parameters.AddWithValue("@city", DropDownList1.SelectedItem.ToString());
                      com.Parameters.AddWithValue("@License", License);
                      com.ExecuteNonQuery();
                      Response.Write("DONE");
                      conn.Close();
                  }
                  catch (Exception ex)
                  { Response.Write("Error:" + ex.ToString()); }
  
          }
  protected void TextBoxUsername_TextChanged(object sender, EventArgs e)
          { // to check if the Username if exist
              if (!string.IsNullOrEmpty(TextBoxUsername.Text))
              {
                  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                  con.Open();
                  SqlCommand cmd = new SqlCommand("select * from DeliveryMen where Username=@Username", con);
                  cmd.Parameters.AddWithValue("@Username", TextBoxUsername.Text);
                  SqlDataReader dr = cmd.ExecuteReader();
                  if (dr.HasRows)
                  {
                      checkusername.Visible = true;
                      imgstatus.ImageUrl = "NotAvailable.jpg";
                      lblStatus.Text = "UserName Already Taken";
                      System.Threading.Thread.Sleep(2000);
  
                  }
                  else
                  {
                      checkusername.Visible = true;
                      imgstatus.ImageUrl = "Icon_Available.gif";
                      lblStatus.Text = "UserName Available";
                      System.Threading.Thread.Sleep(2000);
                  }
              }
              else
              {
                  checkusername.Visible = false;
              }
  
          }
  
          protected void TextBoxEmail_TextChanged(object sender, EventArgs e)
          { // to check if the Email if exist
              if (!string.IsNullOrEmpty(TextBoxEmail.Text))
              {
                  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                  con.Open();
                  SqlCommand cmd = new SqlCommand("select * from DeliveryMen where Email=@email", con);
                  cmd.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
                  SqlDataReader dr = cmd.ExecuteReader();
                  if (dr.HasRows)
                  {
                      Div1.Visible = true;
                      Image1.ImageUrl = "NotAvailable.jpg";
                      Label2.Text = "the Email Already Taken";
                      System.Threading.Thread.Sleep(2000);
  
                  }
                  else
                  {
                      Div1.Visible = true;
                      Image1.ImageUrl = "Icon_Available.gif";
                      Label2.Text = "the Email Available";
                      System.Threading.Thread.Sleep(2000);
                  }
              }
              else
              {
                  Div1.Visible = false;
              }
          }
  </code></pre>