Skip to content

Instantly share code, notes, and snippets.

@kaushalparik27
Created May 14, 2017 08:31
Show Gist options
  • Save kaushalparik27/9b418815186b18c790c76aeccbe88ee9 to your computer and use it in GitHub Desktop.
Save kaushalparik27/9b418815186b18c790c76aeccbe88ee9 to your computer and use it in GitHub Desktop.
jQuery AutoComplete - Basic Configuration - 02
public partial class WebForm69 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//-- actual webmethod that will fetch data from database based on what user typed
[WebMethod]
public static List<Employee> GetEmployeeData(string SearchParam)
{
List<Employee> empList = new List<Employee>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select EmpName, EmpId from EmployeeTable where EmpName like @SearchParam + '%'";
cmd.Parameters.AddWithValue("@SearchParam", SearchParam);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
empList.Add(new Employee() { EmpId = Convert.ToInt32(sdr["EmpName"]), EmpName = Convert.ToString(sdr["EmpId"]) });;
}
conn.Close();
}
}
return empList;
}
//-- generate sample data for auto complete just to demonstrate
[WebMethod]
public static List<Employee> GetEmployeeDataSample(string SearchParam)
{
List<Employee> empList = new List<Employee>();
for (int i = 0; i < 10; i++)
empList.Add(new Employee() { EmpId = i + 1, EmpName = "EmpName " + (i + 1).ToString() });
return empList.Where(record => record.EmpName.ToLower().Contains(SearchParam)).ToList();
}
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment