I would like to ask for very much needed help please, I've been trying to use an Ajax autocomplete extender with a textbox using a SQL database
I've been going through so many examples..copy and pasting every single one into VWD 2005 ..and not even one of the examples work...the textbox just behave as a normal textbox in the browser
Can someone PLEASE PLEASE check my code and see if there's something wrong Please ,I have been trying this out for 3 days now.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<p/>
Type some characters in this textbox. The web service returns random words
that start with the text you have typed.
<br /><br />
' Added to the default web service template as we're using a list to return the auto complete options.
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class Webservice
Inherits System.Web.Services.WebService
'we have to add this line to enable the service to be called from Javascript
'This is where our method starts. Note the name (getemails) is as
'used in the servicemethod when we added the autocomplete
'extender to our .aspx page
'prefixText is the text that the user enters into the textbox
<WebMethod()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer)
Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString
Dim conn As New SqlConnection(connString)
Dim mycommand As New SqlCommand
Dim rs As SqlDataReader
Dim items As New List(Of String)
'we add the prefixText into our SQL query to retrieve the options
'for our autocomplete
Dim strsql As String = "SELECT Keyword FROM Keyword WHERE Keyword LIKE '" & Replace(prefixText, "'", "''") & "%' ORDER BY Keyword ASC"
mycommand.Connection = conn
mycommand.CommandText = strsql
Try
conn.Open()
rs = mycommand.ExecuteReader
If rs.HasRows Then
'we loop through the results adding each result to our list of
'items
Do While rs.Read
items.Add(rs("Keyword"))
Loop
End If
Catch ex As Exception
items.Add(ex.ToString)
' this will show the exception in the autocomplete list if
' there are any errors
If you aren't getting any sort of error back, try putting dummy data in your String List. Your call to sql may not be returning any data. Dummy data will help to identify if that is the problem, or if something else is.
James
James Ashley, Imaginative Universal, Inc
MVP Client App Dev
I'm thinking the same..but I dont know how to create dummy data and bind it to the string in the webservice method. but I do suspect it might be the connection since I get NO error when the browser display the page...
Tell me have you perhaps tried my code?
Thank you so far for your help I do appreciate it so much
Jake have you tried to browse your webservice, is your service returning any result if it does, start a trace and check if data is retrieved from DB when entering values from your searchform (aspx file) if nothing happens now there might be in aspx file
the problem lies.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<div>
<p/>
Type some characters in this textbox. The web service returns random words
that start with the text you have typed.
<br /><br />
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class AutoComplete
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As String()
Dim c1 As Char
Dim c2 As Char
Dim c3 As Char
Ok so now I know this works...but does anyone have a good working webservice code that will retrieve data from a database....I've check the website that
Ameenkpn advised me...its actually one I have check and done...but I'm working in VB and THAT webservice code gives an error in the c# to vb converstion website ( http://www.developerfusion.com/tools/convert/csharp-to-vb/ )
So I only need a cood working database retrieving code
Thanks So so so much for everyone's help so far..I'm one step closer...thanks again for you help
Spider8990
Member
319 Points
354 Posts
Ajax autocomplete extender not working
Oct 20, 2008 04:15 PM|LINK
Hey everyone
I would like to ask for very much needed help please, I've been trying to use an Ajax autocomplete extender with a textbox using a SQL database
I've been going through so many examples..copy and pasting every single one into VWD 2005 ..and not even one of the examples work...the textbox just behave as a normal textbox in the browser
Can someone PLEASE PLEASE check my code and see if there's something wrong Please ,I have been trying this out for 3 days now.
Thank you SO MUCH
Source View CODE
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<p/>
Type some characters in this textbox. The web service returns random words
that start with the text you have typed.
<br /><br />
<asp:TextBox runat="server" ID="myTextBox" Width="300" />
<ajaxToolkit:AutoCompleteExtender
runat="server"
ID="autoComplete1"
TargetControlID="myTextBox"
ServicePath="WebService.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="1"
CompletionInterval="500"
EnableCaching="true"
CompletionSetCount="12" />
</div>
</form>
</body>
</html>
Webservice code
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
' Added to the default web service template as we're using a list to return the auto complete options.
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class Webservice
Inherits System.Web.Services.WebService
'we have to add this line to enable the service to be called from Javascript
'This is where our method starts. Note the name (getemails) is as
'used in the servicemethod when we added the autocomplete
'extender to our .aspx page
'prefixText is the text that the user enters into the textbox
<WebMethod()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer)
Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString
Dim conn As New SqlConnection(connString)
Dim mycommand As New SqlCommand
Dim rs As SqlDataReader
Dim items As New List(Of String)
'we add the prefixText into our SQL query to retrieve the options
'for our autocomplete
Dim strsql As String = "SELECT Keyword FROM Keyword WHERE Keyword LIKE '" & Replace(prefixText, "'", "''") & "%' ORDER BY Keyword ASC"
mycommand.Connection = conn
mycommand.CommandText = strsql
Try
conn.Open()
rs = mycommand.ExecuteReader
If rs.HasRows Then
'we loop through the results adding each result to our list of
'items
Do While rs.Read
items.Add(rs("Keyword"))
Loop
End If
Catch ex As Exception
items.Add(ex.ToString)
' this will show the exception in the autocomplete list if
' there are any errors
Finally
mycommand.Dispose()
conn.Close()
conn.Dispose()
End Try
Return items.ToArray ' we return our list of items as an array
End Function
End Class
----------------------------------------------------------------------------------------------------------------------------------------------
'Just would like to say my Connectionstring is called just "ConnectionString"
and I'm trying to get the data fro a table called = Keyword
with colums KeywordID
Keyword
KeywordHelp
webcofig
<?xml version="1.0"?><configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add namespace="AjaxControlToolkit" assembly="AjaxControlToolkit" tagPrefix="ajaxToolkit"/>
</controls>
</pages>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</system.webServer>
</configuration>
-----------------------------------------------------------------------------------------------
I would appreciate any Help..please
Jake
Ajax autocomplete extender
vgiambattist...
Star
8370 Points
1283 Posts
Re: Ajax autocomplete extender not working
Oct 20, 2008 04:52 PM|LINK
Spider,
Your web method doesn't appear to have a return type. You need to return a string array. I believe the signature should be:
James
MVP Client App Dev
Spider8990
Member
319 Points
354 Posts
Re: Ajax autocomplete extender not working
Oct 20, 2008 05:05 PM|LINK
Hi James
Thank you for your time,.. I've add what you said I should do..But still nothing is happening:-(
Regards Jake
vgiambattist...
Star
8370 Points
1283 Posts
Re: Ajax autocomplete extender not working
Oct 20, 2008 05:34 PM|LINK
Spider,
If you aren't getting any sort of error back, try putting dummy data in your String List. Your call to sql may not be returning any data. Dummy data will help to identify if that is the problem, or if something else is.
James
MVP Client App Dev
Spider8990
Member
319 Points
354 Posts
Re: Ajax autocomplete extender not working
Oct 20, 2008 06:23 PM|LINK
Hey James
I'm thinking the same..but I dont know how to create dummy data and bind it to the string in the webservice method. but I do suspect it might be the connection since I get NO error when the browser display the page...
Tell me have you perhaps tried my code?
Thank you so far for your help I do appreciate it so much
Regards
Jake
marko
Member
152 Points
157 Posts
Re: Ajax autocomplete extender not working
Oct 21, 2008 02:10 AM|LINK
TRY TO PASS THIS IN QUERY ANALYZEER AND SEE IF IT PASSES:
SELECT Keyword FROM Keyword WHERE Keyword LIKE '" & Replace(prefixText, "'", "''") & "%' ORDER BY Keyword ASC
VISUAL STUDIO 2008 PRO
MS SQL SERVER 2005
chetan.sarod...
All-Star
65819 Points
11163 Posts
Re: Ajax autocomplete extender not working
Oct 21, 2008 03:34 AM|LINK
Check web service path i.e. absolute or relative
Also check whether proper recors returning or not from query
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
Exxar
Member
8 Points
4 Posts
Re: Ajax autocomplete extender not working
Oct 21, 2008 09:22 AM|LINK
Jake have you tried to browse your webservice, is your service returning any result if it does, start a trace and check if data is retrieved from DB when entering values from your searchform (aspx file) if nothing happens now there might be in aspx file the problem lies.
ameenkpn
Contributor
4805 Points
814 Posts
Re: Ajax autocomplete extender not working
Oct 21, 2008 10:00 AM|LINK
Compare yours with this article, might solve your problem
http://www.aspdotnetcodes.com/AutoComplete_Textbox_Addtional_Parameters.aspx
Spider8990
Member
319 Points
354 Posts
Re: Ajax autocomplete extender not working
Oct 21, 2008 10:14 AM|LINK
Hey everyone
Thanks a million for your help so far
Well I found one mistake...but there's somewhere one more..
there was a mistake in my aspx file.. like Exar advised me to do check...
so new code for in source view
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<div>
<p/>
Type some characters in this textbox. The web service returns random words
that start with the text you have typed.
<br /><br />
<asp:TextBox runat="server" ID="myTextBox" Width="300" />
<ajaxToolkit:AutoCompleteExtender
runat="server"
ID="autoComplete1"
TargetControlID="myTextBox"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="2"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="12" />
</div>
</form>
</body>
</html>
then I used a autogenerated data code in my webservice... just to check if it links with the ASPX file..
auto generated code in AutoComplete.asmx
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections.Generic
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class AutoComplete
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As String()
Dim c1 As Char
Dim c2 As Char
Dim c3 As Char
If (count = 0) Then
count = 10
End If
Dim rnd As New Random()
Dim items As New List(Of String)
For i As Integer = 1 To count
c1 = CStr(rnd.Next(65, 90))
c2 = CStr(rnd.Next(97, 122))
c3 = CStr(rnd.Next(97, 122))
items.Add(prefixText + c1 + c2 + c3)
Next i
Return items.ToArray()
End Function
End Class
Ok so now I know this works...but does anyone have a good working webservice code that will retrieve data from a database....I've check the website that
Ameenkpn advised me...its actually one I have check and done...but I'm working in VB and THAT webservice code gives an error in the c# to vb converstion website ( http://www.developerfusion.com/tools/convert/csharp-to-vb/ )
So I only need a cood working database retrieving code
Thanks So so so much for everyone's help so far..I'm one step closer...thanks again for you help