jeudi 15 mai 2014

c# - convertir code de service web - Stack Overflow


I have the following code that I need/Want to convert to a webservice.


I get <string xmlns="http://entegral.net/">[]</string> when running web service, no search result. just []


Old Code:


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 onclick=""return OpenWindow('../contactmanagement/contact.aspx?PersonRef=" & objSearchInst.Return1 & "',750,600)""><b>#" & objSearchInst.Return1 & "</b>: " & objSearchInst.Return2 & " " & objSearchInst.Return3 & "</a><br/>"
Next

End If

'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


What I have tried and does not work is:


<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, company.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), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("Mobile") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("ContactNum") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("Category") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dt.Rows.Add(dr)
Next

'Dim objList As New ArrayList
'For Each objSearching In objList
' Dim strText As String = ""
' strText += "<a href='#' class=lnks onclick=""window" & objSearching.Return1 & "',750,600)""><b>#" & objSearching.Return1 & "</b>: " & objSearching.Return2 & " " & objSearching.Return3 & "</a><br/>"
'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

That is what I have tried and also the commented code is what I have tried and does not work. I am very new to web services and want to convert it to web service but I am struggling. I dont mind C# help!



I have the following code that I need/Want to convert to a webservice.


I get <string xmlns="http://entegral.net/">[]</string> when running web service, no search result. just []


Old Code:


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 onclick=""return OpenWindow('../contactmanagement/contact.aspx?PersonRef=" & objSearchInst.Return1 & "',750,600)""><b>#" & objSearchInst.Return1 & "</b>: " & objSearchInst.Return2 & " " & objSearchInst.Return3 & "</a><br/>"
Next

End If

'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


What I have tried and does not work is:


<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, company.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), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("Mobile") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("ContactNum") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("Category") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dt.Rows.Add(dr)
Next

'Dim objList As New ArrayList
'For Each objSearching In objList
' Dim strText As String = ""
' strText += "<a href='#' class=lnks onclick=""window" & objSearching.Return1 & "',750,600)""><b>#" & objSearching.Return1 & "</b>: " & objSearching.Return2 & " " & objSearching.Return3 & "</a><br/>"
'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

That is what I have tried and also the commented code is what I have tried and does not work. I am very new to web services and want to convert it to web service but I am struggling. I dont mind C# help!


0 commentaires:

Enregistrer un commentaire