Click here to Skip to main content
13,772,369 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have given code for Auto Complete Extender but i'm not getting why its not working.

.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Book_Search.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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
   <asp:ScriptManager ID="a" runat="Server" EnablePageMethods="true">
   </asp:ScriptManager>
   <div>
       <asp:TextBox ID="txt" runat="Server" AutoPostBack="True"

           ontextchanged="txt_TextChanged" ></asp:TextBox>
       <asp:AutoCompleteExtender FirstRowSelected="true"

       ServiceMethod="GetTitle"

       MinimumPrefixLength="1"

       CompletionInterval="1000"

       EnableCaching="true"

       CompletionSetCount="2"

       TargetControlID="txt"

       ID="AutoCompleteExtender1" runat="server">
       </asp:AutoCompleteExtender>
       <br />
       <br />
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   </div>
</form>
</body>
</html>


Source code file .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;



namespace Book_Search
{
    public partial class Default : System.Web.UI.Page
    {
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Script.Services.ScriptMethod]
        [System.Web.Services.WebMethod]
        public string[] GetTitle(string prefixText, int count)
        {
            MySqlConnection con = new MySqlConnection(@"server=localhost;user id=root;password=tushar;database=library");
            dt = new DataTable();
            DataSet ds = new DataSet();
            try
            {
                string query = @"Select Title From library.demo d Where Title like '" + prefixText + "%';";
                con.Open();
                MySqlCommand cmd = new MySqlCommand(query, con);
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                //con.Close();
            }
            dt = ds.Tables[0];

            List<string> txtItems = new List<string>();
            string dbValues;

            foreach (DataRow dr in dt.Rows)
            {
                dbValues = dr["Title"].ToString();
                txtItems.Add(dbValues);
            }

            return txtItems.ToArray();
        }
    }
}
Posted 24-May-12 3:10am
Updated 24-May-12 11:20am
v2
Comments
Not Active 24-May-12 8:39am
   
You need to explain more. What isn't working? Is the server method not being called? Are the results returned?
Sandeep Mewara 24-May-12 8:41am
   
'Not working' is less an info to suggest anything.
Jim Jos 24-May-12 9:13am
   
You need to have a separate webservice .asmx file right... Here you have put it under Default.class file which is code behind.. Please confirm you have a separate webservice..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Refer my article hope it helps
Using Ajax AutoCompleteExtender for autosuggest[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

<ajaxToolkit:AutoCompleteExtender

    runat="server"

    ID="autoComplete1"

    TargetControlID="myTextBox"

    ServiceMethod="GetCompletionList"

    ServicePath="AutoComplete.asmx"

    MinimumPrefixLength="2"

    CompletionInterval="1000"

    EnableCaching="true"

    CompletionSetCount="20">
   
</ajaxToolkit:AutoCompleteExtender>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181119.1 | Last Updated 25 May 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100