lingers says:
data:
"{'regno':'"
+ document.getElementById('txtSearch
').value + "'
}",
There is no need to change the parameter value. Only you need to change the parameter name as per your WebMethod parameter name.
data: "{'regno':'" + request.term + "'}",
Refer the below updated code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery Show Records Found Message in AutoComplete</title>
<link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'regno':'" + request.term + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert("Error");
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('#lblUserId').text(ui.item.val);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="demo">
<asp:HiddenField ID="hdnId" runat="server" />
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
<input type="text" id="txtSearch" class="autosuggest" />
</div>
<div> </div>
<div>
Selected UserId:<b><label id="lblUserId" /></b>
</div>
</div>
</form>
</body>
</html>
Code
[WebMethod]
public static List<string> GetAutoCompleteData(string regno)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=NER\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=kaging;"))
{
using (SqlCommand cmd = new SqlCommand("select id,regno from Account where regno LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", regno);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["regno"], dr["id"]));
}
return result;
}
}
}
Screenshot