title: How To Perform Remote Validation in MVC4 Without Using Entity Framework
body: <p>I have developed a web application using MVC without Entity Framework. In my application, I want to perform remote validation to check whether user name already exist or not.I read many blogs about remote validation. In most of the blogs they use entity framework data model to do this whereas I wish to use sql command for that. However I can't achieve the desired validation.</p>
  
  <p>Here is my code:
  Model,Controller,View code.</p>
  
  <p>Mode:</p>
  
  <pre><code>public class RemoteValidation
  {
  
      [Remote("ValidateUserName","Test",ErrorMessage="UserName Already Exist")]
      public string UserName { get; set; }
  
      public string Password { get; set; }
  
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code> [HttpGet]
      public ActionResult RemoteValidation()
      {
          return View();
      }
  
      [HttpPost]
      public ActionResult RemoteValidation(RemoteValidation rv)
      {
          if (ModelState.IsValid)
          {
              SqlConnection conn = new SqlConnection(connString);
  
              conn.Open();
  
              SqlCommand cmd = new SqlCommand("Insert into AdminSignup (UserName,Password) Values ('"+rv.UserName+"','"+rv.Password+"')", conn);
  
              cmd.ExecuteNonQuery();
  
              conn.Close();
          }
  
          return View();
      }
  
  
  
      public JsonResult ValidateUserName(string UserName)
      {
          SqlConnection conn = new SqlConnection(connString);
  
          SqlCommand cmd = new SqlCommand("Select*from AdminSignup Where UserName='"+UserName+"'", conn);
  
          conn.Open();
  
          SqlDataReader dr = cmd.ExecuteReader();
  
          while (dr.Read())
          {
              if (dr.HasRows == true)
              {
                  return Json("User Name Already Exist", JsonRequestBehavior.AllowGet);  
              }
          }
  
          return Json(true, JsonRequestBehavior.AllowGet); 
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model TestMvc.Models.RemoteValidation
  </code></pre>
  
  <p>     </p>
  
  <pre><code>&lt;body&gt;
  
      @using (Html.BeginForm())
      {
          &lt;div&gt;
              @Html.TextBoxFor(m=&gt;m.UserName)
              @Html.ValidationMessageFor(x=&gt;x.UserName)
  
          &lt;/div&gt;
  
          &lt;div&gt;
              @Html.PasswordFor(m=&gt;m.Password)
              @Html.ValidationMessageFor(x=&gt;x.Password)
          &lt;/div&gt;        
              &lt;div&gt;
                  &lt;input type="submit" value="Check" /&gt;
              &lt;/div&gt;
      }
  &lt;/body&gt;
  </code></pre>
  
  <p> </p>
  
  <p>I don't know why i can't get output Any One please help me and Suggest how to acheive this..</p>