Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following code:

I want to convert it to a web service and im not sure how to do it.

VB
Sub Search(ByVal intSearchType As Search.enmSearchType)
        Try

            Dim objSearch As New Search             'search object
            Dim objSearchInst As New Search         'search object
            Dim intResult As Integer                'function call results
            Dim objList As New ArrayList            'array list of search objects

            'hide search panel and check at least 3 characters entered in search field
            panSearch.Visible = True
            If Len(txtSearch.Text) < 3 Then
                txtResults.Text = "Enter at least 3 characters to do a search"
                Exit Sub
            End If

            'do keyword search
            intResult = objSearch.SearchByKeyword(txtSearch.Text, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), intSearchType, objList)

            'if error is returned show user and clear results
            If intResult <> 0 Then
                txtResults.Text = "Oops. There was a problem performing the search, please try again later"
                Exit Sub
            End If
            txtResults.Text = ""

            'show results for contacts
                For Each objSearchInst In objList
                    txtResults.Text += "<a href='#' class=lnks  önclick=""return OpenWindow('../contactmanagement/contact.aspx?PersonRef=" & objSearchInst.Return1 & "',750,600)"">#" & objSearchInst.Return1 & ": " & objSearchInst.Return2 & " " & objSearchInst.Return3 & "</a><br/>"
                Next

            'no results found show user
            If txtResults.Text = "" Then
                txtResults.Text = "no matching records found"
            End If

        Catch ex As Exception
            txtResults.Text = "Oops. There was a problem performing the search, please try again later"
        End Try
    End Sub


I have tried the following:
VB
    <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
    Public Function ContactGet(ByVal searchField As String) As String

        Dim objSearch As New ArrayList
        Dim objSearching As New Search
        Dim intResult As Integer

        Try

            intResult = objSearching.SearchByKeyword(searchField, str_person_ref, str_office_ref, str_organization_ref, _
                                             str_role_ref, compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)

                Dim objContact As New Person
                Dim dt As New DataTable("Contacts")

                Dim col_Name As New DataColumn("Name", GetType(String))
                dt.Columns.Add(col_Name)

                Dim col_Mobile As New DataColumn("Mobile", GetType(String))
                dt.Columns.Add(col_Mobile)

                Dim col_Office As New DataColumn("ContactNum", GetType(String))
                dt.Columns.Add(col_Office)

                Dim col_Category As New DataColumn("Category", GetType(String))
                dt.Columns.Add(col_Category)

                Dim dr As DataRow

                'add new row to datatable
            'For Each objSearching In objSearch

            'For Each drow As DataRow In objSearch
            '    dr = dt.NewRow()
            '    dr("Name") = objContact.FullName
            '    dr("Mobile:") = objContact.MobileNumber
            '    dr("ContactNum") = objContact.OfficeNumber
            '    dr("Category") = objContact.PersonRelationshipType
            '    dt.Rows.Add(dr)
            'Next

            For i = 0 To objSearch.Count - 1
                dr = dt.NewRow()
                dr("Name") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dr("Mobile") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dr("ContactNum") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dr("Category") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dt.Rows.Add(dr)
            Next

            Dim serializer As New JavaScriptSerializer()
            Dim rows As New List(Of Dictionary(Of String, Object))()
            Dim row As Dictionary(Of String, Object) = Nothing

            'serialize dt row to json output
            For Each drow As DataRow In dt.Rows
                row = New Dictionary(Of String, Object)()
                For Each col As DataColumn In dt.Columns
                    row.Add(col.ColumnName, dr(col))
                Next
                rows.Add(row)
            Next

            Dim str_json = JsonConvert.SerializeObject(dt, Formatting.Indented)

            Return str_json
        Catch ex As Exception
            Return Nothing
        End Try
    End Function


When I run the webservice and I invoke it i get:

HTML
<string xmlns="http://compX.net/">[]</string>
and I am not sure what that means.

So basically I want to know is my conversion correct and what does my result mean? Any suggestions?

c# help also welcome
Posted
Updated 5-Apr-14 0:02am
v2
Comments
Sergey Alexandrovich Kryukov 5-Apr-14 6:17am    
Not a question. And makes no sense. There is no such notion as "convert", in this sense. To make a Web service, you need to tell us what your code does and what do you want to achieve by creating a service.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900