Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Is My code

C#
      [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;Integrated Security=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("select Country from Country where Country like @Name+'%'", con);
    cmd.Parameters.AddWithValue("@Name", prefixText);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    List<string> CountryNames = new List<string>();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        CountryNames.Add(dt.Rows[i][1].ToString());
    }
    //return CountryNames;
   return default(string[]);
}


Source Page----

XML
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
              <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
                  DelimiterCharacters="" Enabled="True" ServiceMethod="GetCompletionList"
                  ServicePath="" TargetControlID="TextBox1" UseContextKey="True">
              </cc1:AutoCompleteExtender>
Posted
Updated 15-Dec-12 6:27am
v2
Comments
Dheerendra Dwivedi 16-Dec-12 8:34am    
i want to work without webservice

ServicePath=""
should be like
ServicePath="AutoComplete.asmx"

Without WebService :
ASP.Net Ajax AutoCompleteExtender Without Using Web Service[^]
http://www.aspdotnet-suresh.com/2011/05/ajax-autocompleteextender-sample.html[^]

or else, your code :
C#
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static List<string>GetCompletionList(string prefixText, int count, string contextKey)
    {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select Country from Country where Country like @Name+'%'", con);
        cmd.Parameters.AddWithValue("@Name", prefixText);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<string> CountryNames = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CountryNames.Add(dt.Rows[i][1].ToString());
        }
        return CountryNames;   // returning List<>, and you were returning String[], which is null..
    }
 
Share this answer
 
v3
Hi,

Please refer below code

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="txtcity"
ServicePath="~/WebService.asmx"
ServiceMethod="GetCompletionList"
CompletionInterval="1000"
MinimumPrefixLength="1"
EnableCaching="true"
CompletionSetCount="10"
CompletionListItemCssClass="CssClass"
/>


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

/// <summary>
/// Summary description for WebService
/// </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 WebService : System.Web.Services.WebService
{
Connection con = new Connection();
public WebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count)
{

DataTable dt = con.GetdataTable("SELECT CityName from citymaster Where CityName like '" + prefixText + "%'");
List<string> st = new List<string>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
st.Add(dt.Rows[i]["CityName"].ToString());
}

}

return (st.ToArray());
}

}
 
Share this answer
 

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900