Skip to content
Loading
Display list of first matched values from database in textbo
 .aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Linq;  
  7. using System.Web.Script.Services;  
  8. using System.Web.Services;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. namespace WebApp1  
  12. {  
  13.     public partial class AutoCompleteTextBox : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.   
  18.         }  
  19.   
  20.         [WebMethod]  
  21.         [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
  22.         public static List<string> GetAutoComplete(string pre)  
  23.         {  
  24.             DataTable dtData = new DataTable();  
  25.             string conString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;  
  26.             SqlConnection sqlCon = new SqlConnection(conString);  
  27.             sqlCon.Open();  
  28.             SqlCommand sqlCmd = new SqlCommand("Select BankName From BankMaster Where BankName Like '" + pre + "%'", sqlCon); //  
  29.             SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);  
  30.             sqlDa.Fill(dtData);  
  31.             sqlCon.Close();  
  32.             return (from row in dtData.AsEnumerable() select row["BankName"].ToString()).ToList();  
  33.         }  
  34.     }  

  • Ramesh Palanivel
    Hi Priyanka,
     
    Please check the below link and it can help you to implement the autocomplete functionality
     
    https://www.c-sharpcorner.com/blogs/auto-complete-textbox-using-asp-net-jquery-ajax-call
     
    https://www.aspsnippets.com/Articles/Implement-jQuery-AutoComplete-TextBox-from-database-using-AJAX-PageMethods-in-ASPNet.aspx
     
    http://www.dotnetawesome.com/2013/12/autocomplete-textbox-without-webservice.html 
  •  Hello Priyanka,
    You can use like clause in database query. 
    for eg. 
    select * from table where ColName  Like '[input]%'