title: Ajax autocomple extender from a database
body: <p>I want to create a textbox (for entering names) in my aspx application which suggests names of Employees from database.</p>
  
  <p>I am pasting my code below. I am using Mysql</p>
  
  <p>Default.aspx: </p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  &lt;%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
         &lt;asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"&gt;&lt;/asp:TextBox&gt;
         &lt;asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
                    ServiceMethod="GetCompletionList2" TargetControlID="TextBox1" 
                    UseContextKey="True"&gt;
         &lt;/asp:AutoCompleteExtender&gt;
      &lt;/div&gt;  
      &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;
      &lt;/asp:ScriptManager&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p>Default.aspx.cs:</p>
  
  <pre><code>// here we use a readymade class for database operations to Mysql server
  using System;
  using System.Configuration;
  using System.Data;
  
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  
  
  using Gbs.DAL;  // for ..redymade class..
  
  public partial class _Default : System.Web.UI.Page 
  {
      protected void Page_Load(object sender, EventArgs e)
      {
      }
  
      [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
      public static string[] GetCompletionList2(string prefixText, int count, string contextKey)
      {
          myDb db = new myDb();// its class in Dal.cs
  
          string sql = "Select * from empdata Where empname like @prefixText";
          DataTable dt = db.getDataTable(sql);     // method in redymade class
          string[] items = new string[dt.Rows.Count];
          int i = 0;
          foreach (DataRow dr in dt.Rows)
          {
              items.SetValue(dr["empname"].ToString(), i);
              i++;
          }
          return items;
      }
  
      protected void TextBox1_TextChanged(object sender, EventArgs e)
      {
      }
  }
  </code></pre>
  
  <hr>
  
  <p>// there is no any error, the program runs successfully</p>