Check this example. Now please take its reference and correct your code.
You can change the format as per your choice.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="Stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css" />
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<style type="text/css">
.ui-autocomplete {
max-height: 320px;
overflow-x: auto;
}
</style>
<script type="text/javascript">
$(function () {
$("[id*=txtName]").autocomplete({
minLength: 0,
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Default.aspx/GetDetails") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
}
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.append('<a>'
+ '<table><tr>'
+ '<td style="width:150px">' + item.label + '</td>'
+ '<td style="width:40px">' + item.val + '</td>'
+ '</tr></table></a>')
.appendTo(ul);
};
});
</script>
</form>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Services
Code
C#
[WebMethod]
public static string[] GetDetails(string prefix)
{
List<string> customers = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT CustomerId,ContactName FROM Customers WHERE ContactName LIKE @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerId"]));
}
}
conn.Close();
}
}
return customers.ToArray();
}
VB.Net
<WebMethod>
Public Shared Function GetDetails(ByVal prefix As String) As String()
Dim customers As List(Of String) = New List(Of String)()
Using conn As SqlConnection = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT CustomerId,ContactName FROM Customers WHERE ContactName LIKE @SearchText + '%'"
cmd.Parameters.AddWithValue("@SearchText", prefix)
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(String.Format("{0}-{1}", sdr("ContactName"), sdr("CustomerId")))
End While
End Using
conn.Close()
End Using
End Using
Return customers.ToArray()
End Function
Screeenshot