locked
AutoCompleteExtender & VB.Net RRS feed

  • Question

  • User-224188520 posted

    Hey All, I've been fighting with this all day. I've read a multitude of tutorials and I can not get the AutoCompletedExtender working.

    Here is my aspx page code:

    <asp:TextBox ID="brandTXT" runat="server" AutoPostBack="true"></asp:TextBox>
    
    <asp:AutoCompleteExtender ID="brandTXT_AutoCompleteExtender" runat="server" 
         MinimumPrefixLength="1" CompletionInterval="100" Enabled="True" 
         ServicePath="~/WebService.asmx" EnableCaching="true" 
         CompletionSetCount="20" ServiceMethod="GetCompletionList" TargetControlID="brandTXT">
    </asp:AutoCompleteExtender>

    Here is my WebService.asmx:

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Data.SqlClient
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class WebService
        Inherits System.Web.Services.WebService
        <WebMethod()> _
        Public Function GetCompletionList(prefixText As String, ByVal count As Integer) As List(Of String)
            Dim ConnStr As String = ConfigurationManager.ConnectionStrings("connectionString").ConnectionString
            Dim CnBKPost As New SqlConnection(ConnStr)
    
            Dim result As List(Of String) = New List(Of String)
    
            Dim cmd As New SqlCommand
            cmd.CommandText = "selectBrandByLetter"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = CnBKPost
            cmd.Parameters.AddWithValue("@brand", prefixText)
    
            CnBKPost.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                result.Add(dr("Manufacturer").ToString())
            End While
            CnBKPost.Close()
            Return result
        End Function
    End Class

    The stored procedure is a simple select statement

    SELECT dbo.Inventory.Manufacturer
    	FROM dbo.Inventory
    	WHERE dbo.Inventory.Manufacturer LIKE @brand + '%'
    	GROUP BY dbo.Inventory.Manufacturer
    	ORDER BY dbo.Inventory.Manufacturer

    I can run the stored procedure perfectly with proper results, and I can run a test page with returning the results in a button click command. So I know everything on the database end of things is working fine.

    I've added my service reference for the WebService.asmx page as well to my solution.

    There are no errors, just nothing happens on the page at all...

    I am stumped, any help would be great!

    Tuesday, October 28, 2014 5:26 PM

Answers

  • User-224188520 posted

    Finally got it working... :-)

    1. From project properties I went to WEB then selected local IIS and had it create a virtual directory (http://localhost/projectName).
    2. Added a Service Reference and used my virtual directory as the address.
    3. Changed the Namespace in my asmx file to match the virtual directory.
    4. Used ToolkitScriptManager in my MasterPage not ScriptManager.
    5. And finally as listed below made sure all code was correct.

    aspx page

       <asp:TextBox ID="BrandTXT" runat="server"></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServicePath="http://localhost/Project_HSC_Online/HSCWebService.asmx" ServiceMethod="GetBrandList" 
            EnableCaching="False" MinimumPrefixLength="1" 
            TargetControlID="BrandTXT" CompletionSetCount="10" CompletionInterval="100"></asp:AutoCompleteExtender>

    asmx page

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Data.SqlClient
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://localhost/Project_HSC_Online/HSCWebService.asmx")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class HSCWebService
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        Public Function HelloWorld() As String
            Return "Hello World"
        End Function
    
        <WebMethod()> _
        Public Function GetBrandList(prefixText As String, ByVal count As Integer) As List(Of String)
            Dim ConnStr As String = ConfigurationManager.ConnectionStrings("connectionString").ConnectionString
            Dim CnBKPost As New SqlConnection(ConnStr)
    
            Dim result As List(Of String) = New List(Of String)
    
            Dim cmd As New SqlCommand
            cmd.CommandText = "selectBrandByLetter"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = CnBKPost
            cmd.Parameters.AddWithValue("@brand", prefixText)
            cmd.Parameters.AddWithValue("@count", count)
    
            CnBKPost.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                result.Add(dr("Manufacturer").ToString())
            End While
            CnBKPost.Close()
            Return result
        End Function
    End Class

    Works perfect now, think I'll call it a day and go watch some scarey movies to celebrate Halloween :-)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 31, 2014 12:32 PM

All replies

  • User-1360095595 posted

    Put a breakpoint on the Return result line and check if the result list has any items in it. 

    Wednesday, October 29, 2014 5:26 AM
  • User61956409 posted

    Hi Rivelyn,

    Thanks for your post.

    Firstly, have you get error? if you get the error, you could post back, it will help us to focus on right direction to fix your problem.

    Secondly, you should check whether the ServicePath is correct. If it is correct, you could follow MetalAsp.Net’s suggestion to set breakpoints in your service code to check whether the code is correct.

    Hope it will be helpful to you.

    Best Regards,

    Fei Han

    Wednesday, October 29, 2014 6:05 AM
  • User-224188520 posted

    Put a breakpoint on the Return result line and check if the result list has any items in it. 

    Great suggestion MetalAsp, thank you I'll give it a try and let you know.

    Wednesday, October 29, 2014 7:51 AM
  • User-224188520 posted

    I have tried putting in break points, and I know one thing that is wrong... Nothing is firing at all????

    I can tell by the break points that when I enter anything in the text box nothing at all is happening, it doesn't even seem to be calling the webservice.

    I've got to be missing something, somewhere..

    Thursday, October 30, 2014 8:12 AM
  • User-224188520 posted

    So after a tone of "trial and error" work over the past 24 hours I have finally narrowed it down to my WebService.asmx.

    The AutoCompleteExtender works perfect if I use a code behind page to reference my ServiceMethod. The stored procedure does fire and returns brand results properly.

    So what can I do to find out why my AutoCompeteExtender is not trying to communicate with my WebService.asmx or what can I do to test the WebService.asmx file?

    This is my current code working with a code behind page.

    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
        
        <asp:Panel ID="Panel1" runat="server">
        <asp:TextBox ID="brandTXT" runat="server" Autocomplete="off" AutoPostBack="true"></asp:TextBox>
        <asp:AutoCompleteExtender MinimumPrefixLength="1" CompletionInterval="0" 
        EnableCaching="false" CompletionSetCount="10" 
        TargetControlID="brandTXT" ID="autoCompleteBrandExtender" runat="server" 
        FirstRowSelected="false" ServiceMethod="GetProductList">
        </asp:AutoCompleteExtender>
        </asp:Panel>

    This is the working Code Behind Page.

    Imports System.Data.SqlClient
    
    Public Class Buy_In
        Inherits System.Web.UI.Page
        <System.Web.Script.Services.ScriptMethod> _
    <System.Web.Services.WebMethod> _
        Public Shared Function GetProductList(prefixText As String, ByVal count As Integer) As List(Of String)
            Dim ConnStr As String = ConfigurationManager.ConnectionStrings("conSTR").ConnectionString
            Dim CnBKPost As New SqlConnection(ConnStr)
    
            Dim result As List(Of String) = New List(Of String)
    
            Dim cmd As New SqlCommand
            cmd.CommandText = "selectBrandByLetter"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = CnBKPost
            cmd.Parameters.AddWithValue("@brand", prefixText)
    
            CnBKPost.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                result.Add(dr("Manufacturer").ToString())
            End While
            CnBKPost.Close()
            Return result
        End Function
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
    End Class

    This is my none working WebService.asmx page, and my asp AutoCompleteExtender code bellow that trying to reference my WebService.asmx

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Data.SqlClient
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class HSC_WebService
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        Public Shared Function GetBrandList(prefixText As String, ByVal count As Integer) As List(Of String)
            Dim ConnStr As String = ConfigurationManager.ConnectionStrings("conSTR").ConnectionString
            Dim CnBKPost As New SqlConnection(ConnStr)
    
            Dim result As List(Of String) = New List(Of String)
    
            Dim cmd As New SqlCommand
            cmd.CommandText = "selectBrandByLetter"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = CnBKPost
            cmd.Parameters.AddWithValue("@brand", prefixText)
    
            CnBKPost.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                result.Add(dr("Manufacturer").ToString())
            End While
            CnBKPost.Close()
            Return result
        End Function
    
    End Class
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
        <asp:TextBox ID="brandTXT" runat="server" Autocomplete="off" AutoPostBack="true"></asp:TextBox>
        <asp:AutoCompleteExtender MinimumPrefixLength="1" CompletionInterval="0" 
        EnableCaching="false" CompletionSetCount="10" 
        TargetControlID="brandTXT" ID="autoCompleteBrandExtender" runat="server" 
        FirstRowSelected="false" ServicePath="~/HSC_WebService.asmx" ServiceMethod="GetBrandList">
        </asp:AutoCompleteExtender>

    Here is my Service Reference Screen

    Service Screen Cap

    I don't want to move this to a code behind page for this solution. I really want to get the WebService working properly so I can reference this throughout the project.

    Any help would be great!

    Regards,

    Thursday, October 30, 2014 12:44 PM
  • User-1360095595 posted

    Where is the asmx code located? Also, can you show your @page line from the aspx file. 

    Thursday, October 30, 2014 1:46 PM
  • User-224188520 posted

    asmx file is in the root, same level as the aspx page. I've tried removing the ~/ part of the service path as well just to see, but no change.

    <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/HSCMaster.Master" CodeBehind="Buy-In.aspx.vb" Inherits="Project_HSC_Online.Buy_In" %>

    The master page I left pretty much blank, I included the ToolkitScriptManager and AjaxControlToolkit reference on the actual Buy-In.aspx page. I didn't originally have it that way, but since I've had so much problems with this I scrapped the old project and started new with an empty asp.net 4.0 project just to make sure something in the template was not the cause of any issues I was having.

    Thursday, October 30, 2014 2:06 PM
  • User-224188520 posted

    Just an FYI, this is a reference in my web.config

    <client>
          <endpoint address="http://localhost:2093/BrandAutoComplete.asmx"
            binding="basicHttpBinding" bindingConfiguration="BrandAutoCompleteSoap"
            contract="HSC_WebService.BrandAutoCompleteSoap" name="BrandAutoCompleteSoap" />
        </client>

    Thursday, October 30, 2014 2:43 PM
  • User-224188520 posted

    Sorry, through-out my post you are likely seeing different names, I have been continuously trouble shooting this issue for 2 days now, and I have removed and added code so many times that I am renaming this differently.

    What a mess!!

    Thursday, October 30, 2014 3:14 PM
  • User-224188520 posted

    After carefully re-doing everything, painfully step-by-step this is a summary.

    1. Buy-In.aspx page built and works fine with AutoCompleteExtender.....(confirmed by using Buy-In.aspx.vb page for AutoCompleteExtender source.)
    2. Stored Procedure created and working fine...(used it as datasource in Buy-In.aspx.vb page in step 1)
    3. WebService.asmx created, and... I can run it (image attached)
    4. Webservice presents XML results from string and count entered (image attached)
    5. Successfully added Service Reference to my project and shows in the Service Reference folder.

    Put it all together, and nothing... Auto Complete does not fire when using a asmx file as the datasource...

    Thursday, October 30, 2014 4:08 PM
  • User-1360095595 posted

    I've always used a scriptmanager. Give that a try instead of the toolkitscriptmanager and see if it makes a difference.

    Is the asmx.cs file also located in the root? 

    Thursday, October 30, 2014 5:11 PM
  • User-224188520 posted

    Finally had an error thrown by running my solution using IE.

    iexplore.exe' (Script): Loaded 'Script Code (Windows Internet Explorer)'.
    Unhandled exception at line 120, column 5 in http://localhost:2093/Buy-In.aspx
    0x800a138f - JavaScript runtime error: Unable to get property 'UI' of undefined or null reference

    Thursday, October 30, 2014 5:32 PM
  • User-224188520 posted

    I've used both script and toolkit with no results.

    asmx.vb is also in the root.

    The error that IE was throwing was nothing, I corrected it and still same issue, nothing happening...

    About ready to give up here...lol

    Thursday, October 30, 2014 5:51 PM
  • User-224188520 posted

    When I look at my page source after entering a letter it almost seems that the AutoCompleteExtender is not actually firing.

    <input name="ctl00$MainContent$brandTXT" type="text" value="f" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$MainContent$brandTXT\&#39;,\&#39;\&#39;)&#39;, 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="MainContent_brandTXT" Autocomplete="off" />
    <script type="text/javascript">
    //<![CDATA[
    Sys.Application.add_init(function() {
        $create(Sys.Extended.UI.AutoCompleteBehavior, {"completionInterval":100,"completionSetCount":15,"delimiterCharacters":"","enableCaching":false,"id":"MainContent_brandTXT_AutoCompleteExtender","minimumPrefixLength":1,"serviceMethod":"GetBrandList","servicePath":"http://localhost/Project%20HSC%20Extranet/HSC_WebService.asmx"}, null, null, $get("MainContent_brandTXT"));
    });
    //]]>
    </script>

    There is 2 null values in the service path, I thought they should be "f" and "15"....

    Friday, October 31, 2014 10:15 AM
  • User-224188520 posted

    I can also call the WebService from my code behind. So as of now I am putting sole responsibility on something in my AJAX.

    Friday, October 31, 2014 10:47 AM
  • User-224188520 posted

    Finally got it working... :-)

    1. From project properties I went to WEB then selected local IIS and had it create a virtual directory (http://localhost/projectName).
    2. Added a Service Reference and used my virtual directory as the address.
    3. Changed the Namespace in my asmx file to match the virtual directory.
    4. Used ToolkitScriptManager in my MasterPage not ScriptManager.
    5. And finally as listed below made sure all code was correct.

    aspx page

       <asp:TextBox ID="BrandTXT" runat="server"></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServicePath="http://localhost/Project_HSC_Online/HSCWebService.asmx" ServiceMethod="GetBrandList" 
            EnableCaching="False" MinimumPrefixLength="1" 
            TargetControlID="BrandTXT" CompletionSetCount="10" CompletionInterval="100"></asp:AutoCompleteExtender>

    asmx page

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Data.SqlClient
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://localhost/Project_HSC_Online/HSCWebService.asmx")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class HSCWebService
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        Public Function HelloWorld() As String
            Return "Hello World"
        End Function
    
        <WebMethod()> _
        Public Function GetBrandList(prefixText As String, ByVal count As Integer) As List(Of String)
            Dim ConnStr As String = ConfigurationManager.ConnectionStrings("connectionString").ConnectionString
            Dim CnBKPost As New SqlConnection(ConnStr)
    
            Dim result As List(Of String) = New List(Of String)
    
            Dim cmd As New SqlCommand
            cmd.CommandText = "selectBrandByLetter"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = CnBKPost
            cmd.Parameters.AddWithValue("@brand", prefixText)
            cmd.Parameters.AddWithValue("@count", count)
    
            CnBKPost.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                result.Add(dr("Manufacturer").ToString())
            End While
            CnBKPost.Close()
            Return result
        End Function
    End Class

    Works perfect now, think I'll call it a day and go watch some scarey movies to celebrate Halloween :-)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 31, 2014 12:32 PM