Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Developers,

I am new to Jquery.can any one help me to get usernames from database using jquery.

Regards,
Aravind G
Posted

1 solution

<script type="text/javascript">

$(document).ready(function () {
$("#MainContent_txtsearch").focus(function () {
jQuery("#MainContent_txtsearch").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=uft-8",
url: "Testing.aspx/GetEmployeeName",
data: "{'empname':'" + document.getElementById('MainContent_txtsearch').value + "' }",
dataType: "json",
success: function (data) {
response(data.d)
},
error: function (result) {
}
});
},
minLength: 1,
select: function (event, ui) {
$("#MainContent_txtsearch").val(ui.item.id);

}
}).focus(function () { $(this).autocomplete("search"); });
});
});

</script>



[WebMethod]
public static List<string> GetEmployeeName(string empname)
{

List<string> result = new List<string>();
SqlConnection strcon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connections2sqlserver"].ConnectionString);
strcon1.Open();
SqlCommand cmd = new SqlCommand("select username from TBL_LoginInfo where username like '" + empname + "%'", strcon1);
//open the connection

SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
result.Add(dr["username"].ToString());
}

}
else
{
result.Add("No Reocrds Found");

}
strcon1.Close();
return result;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900