Background
Sometimes there is a need to bind the TextBox Controls from an autocomplete TextBox using ASP.Net C#. Consider a scenario of retail stores that require a product to be auto-populated from the database in a text box when typing and after selecting the product bind other Text Boxes with the product details. In my previous article one of the readers asked me how to fill in textboxes from databases using an auto-complete TextBox Extender, so by considering the preceding requirement I decided to write this article.
- Save DataTable Into ViewState and Bind GridView Without DataBase Using ASP.Net.
- Insert Bulk Records Into DataBase Using ASP.Net C#.
- Creating AutoComplete Extender using ASP.NET.
I hope you have read the preceding articles. Let us start creating an application so beginners can also understand.
- CREATE TABLE [dbo].[ProdcutMaster](
- [ProductId] [int] IDENTITY(1,1) NOT NULL,
- [ProductName] [varchar](50) NULL,
- [BrandName] [varchar](50) NULL,
- [warranty] [int] NULL,
- [Price] [numeric](18, 2) NULL,
- CONSTRAINT [PK_ProdcutsSold] PRIMARY KEY CLUSTERED
- (
- [ProductId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]


- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "FillControlUsingAutoComplete" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - Add Web Form.
- Drag and drop four textBoxes and ScriptManager onto the <form> section of the Default.aspx page.
- Add Ajax AutoComplete Extender from Ajax control Toolkit.
Now the default.aspx page source code will look such as follows.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Article for C#Corner</title>
- </head>
- <body style="background-color: #0000FF">
- <form id="form1" runat="server">
- <h4 style="color: White;">
- Article by Vithal Wadje
- </h4>
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
- </asp:ScriptManager>
- <table style="margin-top: 40px; color: White">
- <tr>
- <td>
- Product Name
- </td>
- <td>
- Brand Name
- </td>
- <td>
- warranty
- </td>
- <td>
- Price
- </td>
- </tr>
- <tr>
- <td>
- <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
- <asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
- CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="TextBox1"
- ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
- </asp:AutoCompleteExtender>
- </td>
- <td>
- <asp:TextBox ID="txtbrandName" runat="server"></asp:TextBox>
- </td>
- <td>
- <asp:TextBox ID="txtwarranty" runat="server"></asp:TextBox>
- </td>
- <td>
- <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- [System.Web.Script.Services.ScriptMethod()]
- [System.Web.Services.WebMethod]
- public static List<string> GetCompletionList(string prefixText, int count)
- {
- return AutoFillProducts(prefixText);
- }
- private static List<string> AutoFillProducts(string prefixText)
- {
- using (SqlConnection con = new SqlConnection())
- {
- con.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
- using (SqlCommand com = new SqlCommand())
- {
- com.CommandText = "select ProductName from ProdcutMaster where " + "ProductName like @Search + '%'";
- com.Parameters.AddWithValue("@Search", prefixText);
- com.Connection = con;
- con.Open();
- List<string> countryNames = new List<string>();
- using (SqlDataReader sdr = com.ExecuteReader())
- {
- while (sdr.Read())
- {
- countryNames.Add(sdr["ProductName"].ToString());
- }
- }
- con.Close();
- return countryNames;
- }
- }
- }
- Create Procedure GetProductDet
- (
- @ProductName varchar(50)
- )
- as
- begin
- Select BrandName,warranty,Price from ProdcutMaster where ProductName=@ProductName
- End
- private void GetProductMasterDet(string ProductName)
- {
- connection();
- com = new SqlCommand("GetProductDet", con);
- com.CommandType = CommandType.StoredProcedure;
- com.Parameters.AddWithValue("@ProductName", ProductName);
- SqlDataAdapter da = new SqlDataAdapter(com);
- DataSet ds=new DataSet();
- da.Fill(ds);
- DataTable dt = ds.Tables[0];
- con.Close();
- //Binding TextBox From dataTable
- txtbrandName.Text =dt.Rows[0]["BrandName"].ToString();
- txtwarranty.Text = dt.Rows[0]["warranty"].ToString();
- txtPrice.Text = dt.Rows[0]["Price"].ToString();
- }
- <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- //calling method and ing Values
- GetProductMasterDet(TextBox1.Text);
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Configuration;
- using System.Data.SqlClient;
- public partial class _Default : System.Web.UI.Page
- {
- public SqlConnection con;
- public SqlCommand com;
- string constr;
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- private void connection()
- {
- constr = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
- con = new SqlConnection(constr);
- con.Open();
- }
- [System.Web.Script.Services.ScriptMethod()]
- [System.Web.Services.WebMethod]
- public static List<string> GetCompletionList(string prefixText, int count)
- {
- return AutoFillProducts(prefixText);
- }
- private static List<string> AutoFillProducts(string prefixText)
- {
- using (SqlConnection con = new SqlConnection())
- {
- con.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
- using (SqlCommand com = new SqlCommand())
- {
- com.CommandText = "select ProductName from ProdcutMaster where " + "ProductName like @Search + '%'";
- com.Parameters.AddWithValue("@Search", prefixText);
- com.Connection = con;
- con.Open();
- List<string> countryNames = new List<string>();
- using (SqlDataReader sdr = com.ExecuteReader())
- {
- while (sdr.Read())
- {
- countryNames.Add(sdr["ProductName"].ToString());
- }
- }
- con.Close();
- return countryNames;
- }
- }
- }
- private void GetProductMasterDet(string ProductName)
- {
- connection();
- com = new SqlCommand("GetProductDet", con);
- com.CommandType = CommandType.StoredProcedure;
- com.Parameters.AddWithValue("@ProductName", ProductName);
- SqlDataAdapter da = new SqlDataAdapter(com);
- DataSet ds=new DataSet();
- da.Fill(ds);
- DataTable dt = ds.Tables[0];
- con.Close();
- //Binding TextBox From dataTable
- txtbrandName.Text =dt.Rows[0]["BrandName"].ToString();
- txtwarranty.Text = dt.Rows[0]["warranty"].ToString();
- txtPrice.Text = dt.Rows[0]["Price"].ToString();
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- //calling method and ing Values
- GetProductMasterDet(TextBox1.Text);
- }
- }





- For detailed code please download the sample Zip file.
- Do proper validation such as date input values when implementing.
- Make the changes in the web.config file depending on your server details for the connection string.
- Add the reference of Ajax Control Toolkit library, if it has not been downloaded then download it from the ASP.Net site.
- Don't forget to set the auto-postback property of the Product Name TextBox to true.

Shahbaz KawarePosted Nov 28, 2017, 3:06 AM
Yes sir i have been checked many times but not getting the data in 2nd textbox
Shahbaz KawarePosted Nov 27, 2017, 9:41 AM
Hello sir im using this code into my 3 tier architecture but im unable to get data on 2nd textbox plz help me asap
Michael VasquezPosted Mar 16, 2017, 2:28 PM
The autopostback does not work on the textbox
Michael VasquezPosted Mar 15, 2017, 4:29 PM
What's the value of count?
Naod AgerePosted Oct 21, 2016, 11:47 AM
Nice article. But I have one question. If I put the same products with different details it just display the first in the list weather i selected the second, third and so on. Can you tell me how I should handle it? Regards
yuki lingPosted Apr 28, 2015, 10:17 PM
Sir,is that the code similar with window form c#?I wanna do something just like your article but in window form.
Vithal WadjePosted Mar 13, 2015, 1:14 PM
yes you can do it,i will write article on it soon
vishal srivastawPosted Mar 13, 2015, 2:42 AM
Sir, can you please guide me with this, if all the controls you used above are inside a gridview row. and i dont want to use server side events. i.e do all the stuff of binding other textboxes using javascript or jquery
Vithal WadjePosted Dec 19, 2014, 11:43 PM
Thanks Saineshwar Bageri sir
Saineshwar BageriPosted Dec 19, 2014, 11:25 PM
Nice article sir
Vithal WadjePosted Dec 19, 2014, 11:14 PM
Thanks Manish Kumar Choudhary sir
Vithal WadjePosted Dec 19, 2014, 11:13 PM
Thanks Jitendra Kumar sir
Manish Kumar ChoudharyPosted Dec 19, 2014, 11:09 PM
Nice explanation Vithal Wadje sir..
Jitendra KumarPosted Dec 19, 2014, 10:55 PM
Nice article..