title: How to check whether username already exists using ajax in asp.net?
body: <p>I am working on an application which has a registration form and I have to display to the user whether the username exists or not.
  I am using asp.net mvc3 and planned to use AJAX to achieve this.</p>
  
  <p>I have a form</p>
  
  <pre><code>&lt;tr&gt;
  &lt;td&gt;User Name*&lt;/td&gt;
  &lt;td&gt;&lt;input id="UserName" name="UserName" type="text" onblur="check(this.value);"/&gt;&lt;/td&gt;
  &lt;td id= "UName"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>which calls a .js file that has the folling contents</p>
  
  <pre><code>function check(User) {
      ...
      var url = "/UserNameCheck/Index";
      url += "?User=" + User;
      xmlHttp.onreadystatechange = state_Change;
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
  }
  function state_Change() {
      if (xmlhttp.readyState == 4) {// 4 = "Response loaded"
  
          if (xmlhttp.status == 200) {// 200 = Response Error Free               
              document.getElementById("UName").innerHTML = xmlHttp.responseText
          }
          else {
              alert("Problem retrieving XML data");
          }
      }
  }
  </code></pre>
  
  <p>I alerted the username and I am getting the correct value that i had entered. Now, the URL is /UserNameCheck/Index where UserNameCheck is a controller and Index is a method in that.</p>
  
  <p>The controller has this code.</p>
  
  <pre><code>public ActionResult Index(string User)
          {
              string UserName;
              try
              {
                  Response.Cache.SetCacheability(HttpCacheability.NoCache);
                  UserName = Request.QueryString["User"];
                  ConnectionPackage.ConnectionClass cc = new ConnectionPackage.ConnectionClass();
                  conn = cc.con;
                  string sql = "Select UserName FROM UserDetails where UserName = '" + UserName + "'";
                  conn.Open();
                  SqlCommand cmd = new SqlCommand(sql, conn);
                  cmd.CommandType = CommandType.Text;
                  object p = cmd.ExecuteScalar();
                  cmd.ExecuteNonQuery();
                  string u = (string)p;
                  if (u.Length==0 || u.Equals("NULL") || u.Equals("null")||u.Equals("Null"))
                  {
                      return View();
                  }
  
                  return null;
              }
              catch (Exception ex){
              }
  </code></pre>
  
  <p>and the view has </p>
  
  <pre><code> String buffer = " &lt;table&gt;&lt;tr&gt;&lt;td id = 'UName' &gt;" This user name already exists. Please select some other unser name.
   buffer = buffer + "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";
   response.getWriter().println(buffer);
  </code></pre>
  
  <p>I also tried writing </p>
  
  <pre><code>Response.Clear();
  Response.Write("UserName already exists. Please select another UserName");
  Response.End();
  </code></pre>
  
  <p>instead of returning View.
  But in both the cases, I didn't get any message that the username exists even though I typed a user name that was already present in the database.</p>
  
  <p>The connection string work for inserting into the database, so I dont think there is a problem with that. Is there a problem with the URL that I have mentioned in the js file? Or is my entire approach wrong? 
  I am basically from java background so dont know much about asp.net. Please do help.</p>
  
  <p>Thank you very much in advance.</p>