简体   繁体   中英

Data is not showing up in Ajax Auto complete in ASp.net C#

I have a text box and i have added AJAX AutocompleteExtender to this.. the code is given below.. From:

ANd is CS file file i have added

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        string connString = ConfigurationManager.ConnectionStrings["Station"].ToString();

        string selectString = "SELECT *from Station";

        List<String> CustList = new List<string>(count);
        using (SqlConnection sqlConn = new SqlConnection(connString))
        {
            sqlConn.Open();

            using (SqlCommand sqlCmd = new SqlCommand(selectString, sqlConn))
            {
                SqlDataReader reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                    CustList.Add(reader["DBRT"].ToString());//DBRT is the Column name

            }
        }
        return (CustList.ToArray());
    }

When i execute the code and type in that it is not showing anything while i type .What is wrong in my code..

With out using web service, this is how i would have done. You can use web service as well if you want. Instead of returing string array i am returning generic List.

In Aspx File

You can change the tag Perfix of Ajax controls according to yours, in my system Ajax control tag perfix start from asp.

<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
</asp:ScriptManager>

<asp:TextBox ID="StationSearchTxtBox" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ServiceMethod="GetCompletionList"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="StationSearchTxtBox"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</asp:AutoCompleteExtender>

in CS file

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]

public static List<string> GetCompletionList(string prefixText, int count)
    {

using (SqlConnection conn = new SqlConnection())
    {
       string connString =    ConfigurationManager.ConnectionStrings   ["Station"].ToString();

        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select StationName from Stations where " +
            "StationName like @SearchStation + '%'";
            cmd.Parameters.AddWithValue("@SearchStation", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> stations= new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["StationName"].ToString());
                }
            }
            conn.Close();
            return stations;
        }
    }

Hope fully this code will give you way to solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM