You can't populate the GridView by calling the displaydata method.
You need to use the Ajax Success function to populate the GridView.
For more details refer below article.
Bind Dataset to ASP.Net GridView using jQuery AJAX
Using the Article i have created an example.
HTML
<asp:TextBox ID="txtEmployeeId" runat="server" onchange="GetEmployee()"></asp:TextBox><br />
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox><br />
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox><br />
<br />
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="true"></asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function GetEmployee() {
$.ajax({
type: "POST",
url: "Default.aspx/GetEmployeeDetails",
data: '{searchText: ' + $('[id*=txtEmployeeId]').val() + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var employees = xml.find("Employee");
var result = xml.find("Results");
var data = result.find("Result").text().split(';');
$('[id*=txtFirstName]').val(data[0].split(':')[1]);
$('[id*=txtLastName]').val(data[1].split(':')[1]);
var row = $("[id*=gvEmployees] tr:last-child").clone(true);
$("[id*=gvEmployees] tr").not($("[id*=gvEmployees] tr:first-child")).remove();
$.each(employees, function () {
$("td", row).eq(0).html($(this).find("EmployeeID").text());
$("td", row).eq(1).html($(this).find("FirstName").text());
$("td", row).eq(2).html($(this).find("LastName").text());
$("[id*=gvEmployees]").append(row);
row = $("[id*=gvEmployees] tr:last-child").clone(true);
});
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet;
SqlDataSource1.ProviderName = "System.Data.SqlClient";
SqlDataSource1.SelectCommand = "SELECT EmployeeID, FirstName, LastName FROM Employees";
gvEmployees.DataSourceID = SqlDataSource1.ID;
gvEmployees.DataBind();
}
}
[WebMethod]
public static string GetEmployeeDetails(string searchText)
{
DataSet ds = new DataSet("Employees");
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
string query = "SELECT EmployeeID, FirstName, LastName FROM Employees WHERE EmployeeID = @EmployeeID";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@EmployeeID", searchText);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable("Employee");
sda.Fill(dt);
ds.Tables.Add(dt);
}
}
}
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@EmployeeID", searchText);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.Read())
{
DataTable dt = new DataTable("Results");
dt.Columns.Add("Result");
dt.Rows.Add(string.Format("First Name:{0};Last Name:{1}", sdr["FirstName"], sdr["LastName"]));
ds.Tables.Add(dt);
}
}
con.Close();
}
}
return ds.GetXml();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings("conString").ConnectionString
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet
SqlDataSource1.ProviderName = "System.Data.SqlClient"
SqlDataSource1.SelectCommand = "SELECT EmployeeID, FirstName, LastName FROM Employees"
gvEmployees.DataSourceID = SqlDataSource1.ID
gvEmployees.DataBind()
End If
End Sub
<WebMethod>
Public Shared Function GetEmployeeDetails(ByVal searchText As String) As String
Dim ds As DataSet = New DataSet("Employees")
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim query As String = "SELECT EmployeeID, FirstName, LastName FROM Employees WHERE EmployeeID = @EmployeeID"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@EmployeeID", searchText)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable("Employee")
sda.Fill(dt)
ds.Tables.Add(dt)
End Using
End Using
End Using
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@EmployeeID", searchText)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
Dim dt As DataTable = New DataTable("Results")
dt.Columns.Add("Result")
dt.Rows.Add(String.Format("First Name:{0};Last Name:{1}", sdr("FirstName"), sdr("LastName")))
ds.Tables.Add(dt)
End If
End Using
con.Close()
End Using
End Using
Return ds.GetXml()
End Function
Screenshot