Saturday, March 24, 2012

Conditional Logic in the Web Service that feeds the AutoComplete extender ? ?

I have a Autocomplete extender that is "tied" to a web service ... the web service basically populates an array of folder names in a directory (these folder names also happen to be server names)

It works great, I get a full list of folder names (servers) with the following code (this is a chunk of the relevant code) ... However I need any of the "If" statements to work, but when I use these, I get nothing when I press keys ... i.e. known folder names

GetServerNames.vb

==============

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections.Generic
Imports System.IO.Directory

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class GetServerNames
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function ReturnServerFolders(ByVal prefixText As String, ByVal count As Integer) As String()
Dim strFolderPath As String = ("C:\SERVER_FILES\")
Dim arrDirectories As String() = System.IO.Directory.GetDirectories(strFolderPath)
Dim strDirectory As String
Dim items As New List(Of String)
For Each strDirectory In arrDirectories
strDirectory = Replace(strDirectory, strFolderPath, "")
'If prefixText = Left(strDirectory, prefixText.Length) Then '<- does not work returns no names
'If strDirectory.StartsWith(prefixText) Then '<- does not work returns no names
'If InStr(strDirectory, prefixText) Then '<- does not work returns no names
items.Add(strDirectory) '<- works always with no if logic returns all folder names
'End If
Next
Return items.ToArray()
End Function

End Class

Default.aspx

=========

<asp:TextBoxID="TextBox1"runat="server"Width="130px"></asp:TextBox>

<cc1:AutoCompleteExtenderID="AutoCompleteExtender1"runat="server"TargetControlID="TextBox1"

ServicePath="GetServerNames.asmx"ServiceMethod="ReturnServerFolders"

MinimumPrefixLength="2"CompletionSetCount="20"CompletionInterval="1000"

EnableCaching="true"Enabled="true">

</cc1:AutoCompleteExtender>

Maybe it's just a case (!) of case sensitivity.

If strDirectory.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase)


Yep, Thanks

That is it ...

Actually the lightbulb went off when I saw this post

http://support.jodohost.com/archive/index.php?t-9497.html

I was coming back here to post a "solved" ... I knew when I saw a reply that would be it ;-)

I am going to go with the InStr function because this field is actually a "Server Name" field ... this could be helpful if looking for a server name with "WEB" in it regardless of what it begins with.

Here is the working ServiceMethod for this

===============================

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections.Generic
Imports System.IO.Directory

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class GetServerNames
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function ReturnServerFolders(ByVal prefixText As String, ByVal count As Integer) As String()
Dim strFolderPath As String = ("C:\SERVER_FILES\")
Dim arrDirectories As String() = System.IO.Directory.GetDirectories(strFolderPath)
Dim strDirectory As String
Dim items As New List(Of String)
For Each strDirectory In arrDirectories
strDirectory = Replace(strDirectory, strFolderPath, "")
If InStr(Ucase(strDirectory), Ucase(prefixText)) Then
items.Add(strDirectory)
End If
Next
Return items.ToArray()
End Function

End Class

No comments:

Post a Comment