简体   繁体   中英

Search Based on AutoComplete Extender Value in asp.net

I have a textBox with AutoCompelte Extender, Autocomplete Text is from Database.

I had Written a Stored Proc for Autocomplete List(In Which I m binding Two Columns from Two Different Tables with crossJoin).

My problem is :

When I select a Value from Autocomplete TextBox and CLick on Search Button, MY Select Query Behind the Button is not Fetching any Value.

MY Code is:

SqlConnection Con = new SqlConnection("DataSource=Localhost;User ID = sa; Password =xxxxx;Initial Catalog =sample;");
SqlCommand cmd = new SqlCommand("Select * from tblCategoryDetails where CategoryName LIKE '"+ TextSearch.Text"' ", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);     
DataTable dt = new DataTable();
da.Fill(dt);

GridView3.DataSource = dt;
GridView3.DataBind();

Can anyone help me on this

That isn't a stored procedure but just a sql command.

SqlCommand cmd = new SqlCommand("Select * from tblCategoryDetails where CategoryName LIKE '"+ TextSearch.Text"' ", Con);

Try to put wild card % within the SqlCommand

("Select * from tblCategoryDetails where CategoryName LIKE '%"+ TextSearch.Text + "%' ", Con);

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> CategoryName(string prefixText, string contextKey)
    {
        //Your Stored Procedure HERE then,
 Your Sql Query where Condition should be 
            where Category Like '%' + @Category  + '%'



       //Return to get List you search
        List<string> Category= new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {

            Category.Add(dt.Rows[i]["Category"].ToString());

        }
        return Category;
    }

In your Markup Code : Inside AutoCompleteExtender : ServiceMEthod should be same as your webmethod class

   <asp:AutoCompleteExtender ID="AutoComp" runat="server" TargetControlID="txtsrch"
      MinimumPrefixLength="3"   EnableCaching="true" CompletionSetCount="1" CompletionInterval="1"
 OnClientItemSelected="ClientItemSelected" ServiceMethod="CategoryName"  CompletionListCssClass="completionListElement"
 CompletionListHighlightedItemCssClass="AutoComplete_ListItemHilite" CompletionListItemCssClass="listItem"                                OnClientPopulated="ClientPopulated" UseContextKey="true" 
OnClientShowing="clientShowingsrch">
                        </asp:AutoCompleteExtender>

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