Bind Label based on TextBox value using jQuery AJAX in ASP.Net

Last Reply 4 months ago By dharmendr

Posted 4 months ago


I have one textbox where user search for e.g 'Nauna'

i want where user finish the typing in textbox it make ajax call without page refresh

select data from sql server Select id, name from person table

bind the data in label

lblid= id -fetch from select statement

lblname=  name- fetch from select statement

if there is no value lbl say no datafound.


i want to fetch data and bind it on label.

all the time select query returns only one row. 

1. textbox user write 'nauna'

2. it returns only one rowa ALWAYS

3. and bind it to labels

You are viewing reply posted by: dharmendr 4 months ago.
Posted 4 months ago Modified on 4 months ago

Hi nauna,

You can pass json string and assign the data.

Check this example. Now please take its reference and correct your code.


    <asp:TextBox runat="server" ID="txtSearch" />
    <br />
    Id :
    <asp:Label ID="lblId" runat="server" />
    <br />
    Name :
    <asp:Label ID="lblName" runat="server" />
<script type="text/javascript" src=""></script>
<script type="text/javascript">
    $(function () {
        $('#txtSearch').on('blur', function () {
            var name = $(this).val();
                type: "POST",
                url: "Default.aspx/GetPersonDetails",
                data: '{name: "' + name + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response.d != '') {
                        var user = JSON.parse(response.d);
                    else {
                        $('#lblId').html('no datafound');
                error: function (response) {



using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Script.Serialization;


Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Script.Serialization



public static string GetPersonDetails(string name)
    string jsonData = "";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId,Name FROM Customers WHERE Name = @Name", con))
            cmd.Parameters.AddWithValue("@Name", name);
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
                var input = new
                        Id = sdr["CustomerId"],
                        Name = sdr["Name"]
                jsonData = (new JavaScriptSerializer()).Serialize(input);

    return jsonData;


Public Shared Function GetPersonDetails(ByVal name As String) As String
    Dim jsonData As String = ""
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId,Name FROM Customers WHERE Name = @Name", con)
            cmd.Parameters.AddWithValue("@Name", name)
            Dim sdr As SqlDataReader = cmd.ExecuteReader()
            If sdr.Read() Then
                Dim input = New With {Key
                    .Id = sdr("CustomerId"), Key
                    .Name = sdr("Name")
                jsonData = (New JavaScriptSerializer()).Serialize(input)
            End If
        End Using
    End Using

    Return jsonData
End Function