Click here to Skip to main content
16,008,010 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
i tried following code for check user name availability using
xmlhttprequest...
but is not working..
plz tell where i m wrong..

1. check.aspx

C#
protected void Page_Load(object sender, EventArgs e)
   {
       string StuName = Request.QueryString["stuname"] as string;

       if (StuName != null)
       {
           DataTable table = DBHelper.GetStuList(StuName.Trim());

           string result = string.Empty;

           foreach (DataRow r in table.Rows)
           {
               result += r["StuName"].ToString() + ";";
           }

           Response.Clear();
           Response.Write(result);
           Response.End();
       }



2. default.aspx

C#
function Updatename()
{

    xmlHttp = null;

    if(window.XMLHttpRequest)
    {

        xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {

        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(xmlHttp != null)
    {

        var stuName = document.getElementById('<#=txtStuName.ClientID #>').value;


        xmlHttp.onreadystatechange=state_Change;


        xmlHttp.open("GET","check.aspx?StuName="+stuName,true);
        xmlHttp.send(null);
    }
}


function state_Change()
{
    if (xmlHttp.readyState==4)
    {

        if (xmlHttp.status==200)
        {

            var stuname = xmlHttp.responseText;
            var length = stuname.length;

            alert(stuname)
        }
    }
}
Posted

1 solution

1. default.aspx

C#
function test(a)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
      xmlhttp=new XMLHttpRequest();
    }
    else
    {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
         alert(xmlhttp.responseText);
         document.getElementById("txtStuName").innerHTML=xmlhttp.responseText;
         document.getElementById("txtCellNo").innerHTML=xmlhttp.responseText;
    }
}
var url = "check.aspx?UserName=" +a;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}



2. check.aspx

C#
protected void Page_Load(object sender, EventArgs e)
   {
       conn.Open();
       string s = Request.QueryString.Get("UserName").ToString();

       if (!string.IsNullOrEmpty(s))
       {
           string str = "select * from StuInfo where StuName = '" + s + "'";
           cmd = new SqlCommand(str, conn);

           string status = null;

           SqlDataReader dr = cmd.ExecuteReader();
           if (dr.HasRows)
           {
               status = "duplicate uaername";
           }
           else
           {
               status = "username available";
           }

           Response.Write(status);
           conn.Close();

        }
       else
       {
           string status = "Name should not be null";
            Response.Write(status);
       }

   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900