Click here to Skip to main content
15,905,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form where i am using one autocomplete text box(using ajax autocomplete extender).Autocomplete functionality is working properly.But When I try to fetch data from database and try to display it in my form (say for editing a record) values doesnt display on form.As soon as i comment ajax autocomplete extender from page,all values gets displayed.Why is this happening? I need that autocomplete functionality in my form.Help Me.

XML
<asp:TextBox ID="txtContactsSearch" runat="server" autopostback="True"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtContactsSearch"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>

public void getdata()
{
 Datatable dt=objdal.getdata();
 Datarow dr=dt=.Rows[0];
 txtContactsSearch.Text=dr["contact"].Tostring();
  //sililar code for remaining textboxes on form
}
Posted
Updated 24-Sep-14 21:12pm
v3

1 solution

XML
Search : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

      <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
                  ServiceMethod="AutoCompleteAjaxRequest"
                  ServicePath="AutoComplete.asmx"
                  MinimumPrefixLength="2"
                  CompletionInterval="100"
                  EnableCaching="false"
                  CompletionSetCount="10"
                  TargetControlID="TextBox1"
                  FirstRowSelected="false">
      </asp:AutoCompleteExtender>

SQL
In above code you can see that I have specified the TargetControlID="TextBox1" . This tag will used for specifying that in which control auto suggest will appear.

Now we will see hw to receive data from data base. For this in our AutoCompleteExtender tag we have two properties ServiceMethod and ServicePath . In this we will define web service name and method . For this add a webservice file in your project (.asmx). So here is the code of you webservice.



XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;

namespace ProjectDemo_Asp.et
{
    /// <summary>
    /// Summary description for AutoComplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class AutoComplete : System.Web.Services.WebService
    {

        [WebMethod]
        public string[] AutoCompleteAjaxRequest(string prefixText, int count)
        {
            List<string> ajaxDataCollection = new List<string>();
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase(prefixText);
                if(_objdt.Rows.Count>0)
                {
                    for (int i = 0; i < _objdt.Rows.Count; i++)
                    {
                        ajaxDataCollection.Add(_objdt.Rows[i]["LanguageName"].ToString());
                    }
                }
            return ajaxDataCollection.ToArray();
        }
        /// <summary>
        /// Function for retriving data from database
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase(string prefixText)
        {
            string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
            DataTable _objdt = new DataTable();
            string querystring = "select * from ProLanguage where LanguageName like '%" + prefixText + "%';";
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
    }
}



read this you may get more clearity

http://www.dotnetfox.com/articles/ajax-autocomplete-example-with-database-in-Asp-Net-1079.aspx[^]
 
Share this answer
 
v2

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