my AutoComplete is not showing the right names when i type alphabetes.
For example, if i type M it will show autocomplete of names with M, if i add c to M it will show names with MC. but my autocomplete is not that way, if i type M sometimes it doesnt show the names with it
<script type="text/javascript">
$(function () {
$("[id$=SearchText]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Default2.aspx/GetNames") %>',
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);
}
});
},
select: function (e, i) {
$("[id$=hfUserName]").val(i.item.val);
},
minLength: 1
});
});
</script>
code
public partial class Home : System.Web.UI.Page
{
[WebMethod]
public static string[] GetNames(string prefix)
{
List<string> users = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings[""].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, UserName from User3 where " + "UserName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
users.Add(string.Format("{0}-{1}", sdr["Name"], sdr["UserName"]));
}
}
conn.Close();
}
}
return users.ToArray();
}