Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

i want to implement the auto complete text box in asp.net 3.5 using ajax with my database.I have a sample code which is using ajaxtoolkit.dll but it is not working ,i dont know what happen why it is not working ,i thought that i have missing some thing in the code but what ican not caught it out.As i am new to ajax .So plz help me

my code as follows:
Default.aspx


XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="ajaxToolkit"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.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="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="AutoCompleteTest.asmx" />
            </Services>
        </asp:ScriptManager>
        <div>
            <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
            <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoCompleteTest.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1" EnableCaching="true" />
        </div>
    </form>
</body>
</html>



[web service AutoCompleteTest.asmx]

AutoCompleteTest.cs

MSIL
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for AutoCompleteTest
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 AutoCompleteTest : System.Web.Services.WebService {
    public AutoCompleteTest () {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod]
    public string[] GetCountriesList(string prefixText)
    {
        DataSet dtst = new DataSet();
        SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["AppConnString"]);
        string strSql = "SELECT Name FROM Colleges WHERE Name LIKE '" + prefixText + "%' ";
        SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
        sqlCon.Open();
        SqlDataAdapter sqlAdpt = new SqlDataAdapter();
        sqlAdpt.SelectCommand = sqlComd;
        sqlAdpt.Fill(dtst);
        string[] cntName = new string[dtst.Tables[0].Rows.Count];
        int i = 0;
        try
        {
            foreach (DataRow rdr in dtst.Tables[0].Rows)
            {
                cntName.SetValue(rdr["Name"].ToString(), i);
                i++;
            }
        }
        catch { }
        finally
        {
            sqlCon.Close();
        }
        return cntName;
    }
}


I have use AjaxToolkit .dll which is in bin folder.I have provided the code plz tell me what is wrong in this
Posted

// 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 AutoCompleteTest : System.Web.Services.WebService


Does it work if you uncomment the line in the webservice as above?
 
Share this answer
 
Comments
nishi2010 14-Jun-10 14:26pm    
i had uncomment that line also but still it is not working
You should use string prefixtext and int count as the parameters of the service method because you are using ajax control toolkit.

//Count is used to specify the number of suggestions shown.
public string[] GetCountriesList(string prefixText, int count)
 
Share this answer
 
Comments
arprakash 16-Feb-12 5:04am    
fine work

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