Hi micah,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="WebForm7.aspx.cs" Inherits="WebForm7" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
EmpName :
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
<asp:HiddenField ID="hfCustomerId" runat="server" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="Stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function () {
$("[id*=txtEmpName]").autocomplete({
source: function (request, response) {
var param = { empName: request.term };
$.ajax({
url: '<%=ResolveUrl("~/WebForm7.aspx/GetEmpNames") %>',
data: JSON.stringify(param),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item,
val: item
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id*=hfCustomerId]").val(i.item.val);
},
minLength: 1
});
});
</script>
</asp:Content>
Namepsaces
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 List<string> GetEmpNames(string empName)
{
List<string> Emp = new List<string>();
string query = string.Format("SELECT ContactName FROM Customers WHERE ContactName LIKE '%{0}%'", empName);
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Emp.Add(reader.GetString(0));
}
}
}
return Emp;
}
VB.Net
<WebMethod>
Public Shared Function GetEmpNames(ByVal empName As String) As List(Of String)
Dim Emp As List(Of String) = New List(Of String)()
Dim query As String = String.Format("SELECT ContactName FROM Customers WHERE ContactName LIKE '%{0}%'", empName)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Emp.Add(reader.GetString(0))
End While
End Using
End Using
Return Emp
End Function
Screenshot