locked
Text box Asynchronous RRS feed

  • Question

  • User520965484 posted

    How to do Auto Synchronise to text box from database and check the entered data is existence or not.. 

    Thursday, September 10, 2015 2:50 PM

Answers

  • User2103319870 posted

    How to do Auto Synchronise to text box from database and check the entered data is existence or not.. 

    You can use the Jquery Ajax Method to check if the ID value is available in database or not . You can call the method to check ID on "onBlur" event of textbox. Below code which I posted earlier is looking for an ID in database. With some small changes you can make it working as per your requirement

    HTML

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
    
            function checkIDAvailability() {
                $.ajax({
                    type: "POST",
                    url: "JqueryAjaxPost.aspx/checkUserName",
                    data: "{IDVal: '" + $("#<% =txtID.ClientID %>").val() + "' }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: onSuccess,
                    failure: function (AjaxResponse) {
                        document.getElementById("Label1").innerHTML = "Dfgdfg";
                    }
                });
                function onSuccess(AjaxResponse) {
                    document.getElementById("Label1").innerHTML = AjaxResponse.d;
                }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Enter your ID here
            <asp:TextBox ID="txtID" runat="server" onblur="checkIDAvailability();"></asp:TextBox>
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
        </form>
    </body>
    </html>

    C#:

    This method will get called from clientside using Jquery Ajax

    //Server Side Method which will get called from Client Side
            //Ensure that you have declared the method like WebMethod
            [System.Web.Services.WebMethod(EnableSession = true)]
            public static string checkUserName(string IDVal)
            {
                string result = string.Empty;
                //Get your connection string here
                string conString = System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks2008R2ConnectionString2"].ConnectionString;
                //Change your query here
                string qry = "Select AddressID from Person.Address Where AddressID =@AddressID";
                SqlDataAdapter da = new SqlDataAdapter(qry, conString);
                //Pass the value to paramter
                da.SelectCommand.Parameters.AddWithValue("@AddressID", IDVal.Trim());
                DataSet ds = new DataSet();
                da.Fill(ds, "IDTable");
                //Check if dataset is having any value
                if (ds.Tables["IDTable"].Rows.Count > 0)
                {
                    // User Name Not Available
                    result = "ID already in use";
                }
                else
                {
                    //User_Name is available
                    result = "ID is available, you can use it";
                }
                //Return the result
                return result;
            }

    Note: I have used AdventureWorks database, you need to make changes as per your database.

    Demo

    You can get more details from this link  : How to check the ID availability in Database using Jquery Ajax.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 10, 2015 2:57 PM
  • User61956409 posted

    Hi Sravan0910,

    Welcome to ASP.NET forum.

    How to do Auto Synchronise to text box from database and check the entered data is existence or not.. 

    Firstly, it seems that you’d like to check whether user input exists in your database, a2h has shared us a solution to validate user input using AJAX, please refer to his solution.

    Besides, you could refer to these threads that discussed a similar issue.

    http://stackoverflow.com/questions/21760761/check-username-availability-using-jquery-ajax-webmethod-in-asp-net-and-show-erro

    http://codekea.com/nqxALBdWXjpD/check-username-availability-using-jquery-ajax-webmethod-in-aspnet-and-show-error-with-toastr.html

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 4:03 AM

All replies

  • User2103319870 posted

    How to do Auto Synchronise to text box from database and check the entered data is existence or not.. 

    You can use the Jquery Ajax Method to check if the ID value is available in database or not . You can call the method to check ID on "onBlur" event of textbox. Below code which I posted earlier is looking for an ID in database. With some small changes you can make it working as per your requirement

    HTML

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
    
            function checkIDAvailability() {
                $.ajax({
                    type: "POST",
                    url: "JqueryAjaxPost.aspx/checkUserName",
                    data: "{IDVal: '" + $("#<% =txtID.ClientID %>").val() + "' }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: onSuccess,
                    failure: function (AjaxResponse) {
                        document.getElementById("Label1").innerHTML = "Dfgdfg";
                    }
                });
                function onSuccess(AjaxResponse) {
                    document.getElementById("Label1").innerHTML = AjaxResponse.d;
                }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Enter your ID here
            <asp:TextBox ID="txtID" runat="server" onblur="checkIDAvailability();"></asp:TextBox>
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
        </form>
    </body>
    </html>

    C#:

    This method will get called from clientside using Jquery Ajax

    //Server Side Method which will get called from Client Side
            //Ensure that you have declared the method like WebMethod
            [System.Web.Services.WebMethod(EnableSession = true)]
            public static string checkUserName(string IDVal)
            {
                string result = string.Empty;
                //Get your connection string here
                string conString = System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks2008R2ConnectionString2"].ConnectionString;
                //Change your query here
                string qry = "Select AddressID from Person.Address Where AddressID =@AddressID";
                SqlDataAdapter da = new SqlDataAdapter(qry, conString);
                //Pass the value to paramter
                da.SelectCommand.Parameters.AddWithValue("@AddressID", IDVal.Trim());
                DataSet ds = new DataSet();
                da.Fill(ds, "IDTable");
                //Check if dataset is having any value
                if (ds.Tables["IDTable"].Rows.Count > 0)
                {
                    // User Name Not Available
                    result = "ID already in use";
                }
                else
                {
                    //User_Name is available
                    result = "ID is available, you can use it";
                }
                //Return the result
                return result;
            }

    Note: I have used AdventureWorks database, you need to make changes as per your database.

    Demo

    You can get more details from this link  : How to check the ID availability in Database using Jquery Ajax.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 10, 2015 2:57 PM
  • User61956409 posted

    Hi Sravan0910,

    Welcome to ASP.NET forum.

    How to do Auto Synchronise to text box from database and check the entered data is existence or not.. 

    Firstly, it seems that you’d like to check whether user input exists in your database, a2h has shared us a solution to validate user input using AJAX, please refer to his solution.

    Besides, you could refer to these threads that discussed a similar issue.

    http://stackoverflow.com/questions/21760761/check-username-availability-using-jquery-ajax-webmethod-in-asp-net-and-show-erro

    http://codekea.com/nqxALBdWXjpD/check-username-availability-using-jquery-ajax-webmethod-in-aspnet-and-show-error-with-toastr.html

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 4:03 AM