Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

AutoCompleteExtender service

$
0
0

I have the following markup in my aspx webforms page to use for autocomplete textbox. I want it to get matching results from an sql stored procedure (also listed below). The stored procedure requires 2 variables and I cannot get it to work. Any help is appreciated.

p.s. If jquery code is better please point me to sample.

<asp:Label ID="LblAddFoods" runat="server" Text="Enter food name to search" CssClass="Show"></asp:Label><asp:TextBox ID="txtFoodSearch" runat="server" CssClass="Show"></asp:TextBox><asp:AutoCompleteExtender ID="aceFoodSearch" runat="server" 
                                ServiceMethod="SearchFoods" 
                                MinimumPrefixLength="3" 
                                TargetControlID="txtFoodSearch" 
                                CompletionInterval="100" 
                                CompletionSetCount="11" EnableCaching="False"></asp:AutoCompleteExtender>

Below is my webservice

Imports System.Data.SqlClient
Imports System.Web.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

<System.Web.Script.Services.ScriptService()><WebService(Namespace:="http://tempuri.org/")><WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)><Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public Class WebServiceFoods
    Inherits System.Web.Services.WebService<WebMethod()>
    Public Shared Function SearchFoods(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim lstfoods As List(Of String) = New List(Of String)
        Using conData As SqlConnection = New SqlConnection(DBClass.GetKDConnectionString)
            conData.Open()
            Dim strSQL As String = "EXEC kd_selFoodItemsSearch @SearchText ='" & prefixText & "'"
            Dim cmd As SqlCommand = New SqlCommand
            cmd.CommandText = strSQL
            cmd.Parameters.AddWithValue("@SearchText", prefixText)
            cmd.Connection = conData
            Dim sdr As SqlDataReader = cmd.ExecuteReader
            While sdr.Read
                lstfoods.Add(sdr("FoodItem").ToString)
            End While
            sdr.Close()
        End Using

        Return lstfoods
    End Function

End Class

Below is my stored procedure

ALTER PROCEDURE [dbo].[kd_selFoodItemsSearch]
	@OrgID		int,
	@SearchText nvarchar(100) = null
AS 

	BEGIN
	
		IF @SearchText IS NOT NULL
			BEGIN
				SET @SearchText = '%' + @SearchText + '%';
			END
		SELECT F.FoodID, 
			   F.CategoryID, 
			   F.FoodItem
		FROM dbo.tblFoods AS F
		WHERE (F.OrgID = 0 OR F.OrgID = @OrgID)
		  AND (CASE WHEN @SearchText IS NULL THEN 'T'
					WHEN @SearchText IS NOT NULL AND F.FoodItem LIKE @SearchText AND (F.OrgID = 0 OR F.OrgID = @OrgID) THEN 'T'
					ELSE 'F'
					END = 'T')
		ORDER BY F.FoodItem;
	END


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>