title: Display error message in a correct way if username already exists in a registration webform (asp.net)
body: <p>I am able to check if username already exists using a code like this:</p>
  
  <pre><code> SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Job_Registration_ConnectionString"].ConnectionString);
      try
      {
          conn.Open();
          String checkUsr_query = "SELECT count(*) from Job_UserData where username = @Usn";
          SqlCommand comm = new SqlCommand(checkUsr_query, conn);
          comm.Parameters.AddWithValue("@Usn", TextBox_Usn.Text);
          if (!comm.ExecuteScalar().ToString().Equals("0"))
          {
              Label_Usn.Visible = true;
          }
          else
          {
              Label_Usn.Visible = false;
          }
  
      }
      catch (Exception check_usr)
      {
          Response.Write(check_usr.ToString());
      }
      finally
      {
          if (conn != null)
              conn.Close();
      }
  }
  </code></pre>
  
  <p>Other variations don't use a separate label, instead they change the text in <code>RequiredFieldValidator</code> or I can use it with <code>CustomValidator</code> and changing its visibility.<br>
  <strong>What I want to do is</strong> check if textboxes are empty when submit button is pressed which is taken care by <code>RequiredFieldValidator</code> and as soon as I leave the Username textbox it should check and display whether the username is available or not and the <em>Submit</em> click should not allow the user to register.</p>
  
  <p>Currently I am using this code:</p>
  
  <pre><code> protected void Button_RegForm_Submit_Click(object sender, EventArgs e)
  {
    /*  if (Label_Usn.Visible)
      {
          return;
      }
     * */
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Job_Registration_ConnectionString"].ConnectionString);
      try
      {
          String insert_query = "INSERT into Job_UserData VALUES (@Usn,@Pass,@Village,@Contact,@Address,@Occupation)";
          conn.Open();
          SqlCommand comm = new SqlCommand(insert_query, conn);
          comm.Parameters.AddWithValue("@Usn", TextBox_Usn.Text);
          comm.Parameters.AddWithValue("@Pass", TextBox_Pass.Text);
          comm.Parameters.AddWithValue("@Village", TextBox_Village.Text);
          comm.Parameters.AddWithValue("@Contact", TextBox_Contact.Text);
          comm.Parameters.AddWithValue("@Address", TextBox_Address.Text);
          comm.Parameters.AddWithValue("@Occupation", TextBox_Occupation.Text);
          comm.ExecuteNonQuery();
      }
      catch (Exception insertError)
      {
          Response.Write(insertError.ToString());
      }
      finally
      {
          if (conn != null)
              conn.Close();
      }
  }
  
  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
  {
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Job_Registration_ConnectionString"].ConnectionString);
      try
      {
          conn.Open();
          String checkUsr_query = "SELECT count(*) from Job_UserData where username = @Usn";
          SqlCommand comm = new SqlCommand(checkUsr_query, conn);
          comm.Parameters.AddWithValue("@Usn", TextBox_Usn.Text);
          if (!comm.ExecuteScalar().ToString().Equals("0"))
          { 
              args.IsValid = false;
  
          }
          else
          {
              args.IsValid = true;
          }
  
      }
      catch (Exception check_usr)
      {
          Response.Write(check_usr.ToString());
      }
      finally
      {
          if (conn != null)
              conn.Close();
      }
  }
  </code></pre>
  
  <p>And I have to use <code>AutoPostBack</code> for Username textbox to make sure that Duplicate username is checked as soon as I get out of the Username textbox. The code I posted works without <code>AutoPostBack</code> but it lets user to register even after displaying that username is not available and it doesn't check for duplicate username without clicking the <em>Submit</em> button.</p>
  
  <p>When I am using <code>AutoPostBack</code> property it created problem with the <code>RequiredFieldValidator</code> property as if the field is empty the page refreshes due to <code>PostBack</code> after I leave the username textbox and the "Username is required" message flashes for a second and then disappears which I don't want.
  I tried putting the username checking part in <code>Page_Load</code> method and used text change property for <code>RequiredFieldValidator</code>, but sometimes it displays wrong message.</p>
  
  <p>How can I accomplish this or is it done using <code>Page_load</code> method but I am doing it wrong? Is there a better way of doing this?<br>
  The way I want this form to work is how I believe other online forms work, if they work differently please share that method. </p>