简体   繁体   中英

Ajax AutcompletExtender not returning any results

I have a text box, a script manager and an auto-complete extender on an aspx page. I'm trying to simply have an auto complete into the text box and am currently getting nothing. What am I missing? When I try to debug the code the break point never gets beyond the first curly brace in the GetComplete method. I'm on VS2010

markup:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="AjaxAutocomplete._Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
        DelimiterCharacters="" Enabled="True" ServiceMethod="GetComplete" MinimumPrefixLength="3" TargetControlID="TextBox1">
    </asp:AutoCompleteExtender>

</asp:Content>

code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace AjaxAutocomplete
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [System.Web.Script.Services.ScriptMethod]
        [System.Web.Services.WebMethod]
        public static List<string> GetComplete(string Prefixtetxt)
        {
            List<string> returnList = new List<string>();
            string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(cs))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter p = new SqlParameter("@drug_name", Prefixtetxt);
                    //cmd.Parameters.AddWithValue("@drug_name", p);
                    cmd.Parameters.Add("@drug_name", SqlDbType.VarChar);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        returnList.Add(Convert.ToString(reader["drug_name"]));

                    }
                }


            }
          return returnList;

        }
    }
}

sproc from Sql Server

create proc spFindAllDrugs
@drug_name varchar(100)
as
begin
select distinct drug_name
    from rx 
    where drug_name like '%' + @drug_name + '%'
end

It looks like you are not associating the SqlCommand with the connection or telling the command the name of your stored procedure. Try modifying your code as shown.

        using (SqlCommand cmd = new SqlCommand("spFindAllDrugs"))
        {
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;

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