The problem: I have a web service up and running ok, the problem I have is that i need to return more than one instance of a class and have no real idea how to do this.
I have a loop set up in the service:
If localtab.Rows.Count > 0 Then
Do While i <= localtab.Rows.Count
Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
Mbr.County = localtab.Rows(i).Item(8) & vbNullString
Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
Mbr.msg = "Success"
i = i + 1
Loop
Else
Mbr.msg = "Fail - no record found"
End If
And i know that this works fine as it returns the last member listed in the DB. Doing something like Mbr(i).urn etc… won’t work as when I loop through i, it is only ever going to return the first instance, and once the service has returned once it will stop.
Below is the code from the client end calling the service – simple enough, all I am looking for just now is for a message box with each surname to be displayed.
Dim abMem As New ArdbegMember
Dim retMem As ArdbegMember
abMem.Downloaded = "N"
Try
cc.Open()
retMem = cc.MbrReq(abMem)
MesgBox(retMem.Surname)
cc.Close()
What I need to know is how to pass a full recordset back to the client
EDIT
So based on the suggestions below, my code now looks like this –
Dim results As List(Of ArdbegMember)
Dim i As Integer = 0
'assign values from the table to the ArdbegMember object
If localtab.Rows.Count > 0 Then
Do While i <= localtab.Rows.Count
Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
Mbr.County = localtab.Rows(i).Item(8) & vbNullString
Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
Mbr.msg = "Success"
i = i + 1
results.Add(Mbr)
Loop
Else
Mbr.msg = "Fail - no record found"
End If
'Tidy up
dataAdapter.Dispose()
Cmd.Dispose()
oConn.Close()
'Return the ArdbegMember object
Return results
And the error being thrown is now –
Value of type 'System.Collections.Generic.List(Of ArdbegWeb.ArdbegMember)' cannot be converted to 'ArdbegWeb.ArdbegMember'.
Thanks for your help marc, the solution was blindingly obvious and i shouldn’t have missed it!
Do While i <= localtab.Rows.Countshould have readDo While i <= localtab.Rows.Count-1. The loop was repeating on itself one too many times causing issues when it was trying to return Mbr to the client side!