We help IT Professionals succeed at work.

return multiple value in webservices in vb.net

Bakersville
Bakersville asked
on
Hello everyone.

Been working on this for about 2 weeks now and done some things great, but can't get it to do above.

I have created a web service that will insert any data i want into a sql database, however, when i try and retreive data from the database using what i know, i only get the last record rather than all 20 records returning.

So, i thought i would be clever and get it to return the value everytime it loop through the records, however, it only bring up the first record, not the last.

Can any help me try and get it to display all 20 records when i involke the service. From there, i should be able to display it in another application.

Code below

Thank you in advance
Imports System.Web.Services
Imports System.Data.SqlClient


<WebService(Namespace:="http://website.co.uk/")> Public Class TeamList

#Region " Web Services Designer Generated Code"

#End Region

    <WebMethod()> Public Function TeamDisplay()

        'Connect to DB and get the XML code for the teams

        Dim conn As SqlConnection
        
        conn = New SqlConnection("Data Source=localhost;Initial Catalog=DatabaseTest;Persist Security Info=True;User ID=user;Password=password")

        Dim cmd As SqlCommand

        cmd = New SqlCommand("select Team from CalenderTeam", conn)
        



        Try
            conn.Open()
            Dim rdr As SqlDataReader
            rdr = cmd.ExecuteReader

            If rdr.HasRows Then

                While rdr.Read()

                    Return Display1

                End While

            Else


            End If

        Finally
            conn.Close()
        End Try



    End Function




End Class

Open in new window

Comment
Watch Question

Software Architect
Commented:
The "Return Display1" causes the function to exit and return the contents of Display1 to the caller. Return doesn't return something and then keep processing. The only way to do what you want is to collect all the values you want returned into some sort of structure and then return that. If the client that is consuming your web service is a .NET client and always will be a .NET client then you could use a DataTable (you'll need to create a SqlDataAdapter and fill it rather than using a SqlDataReader). If the client could be something other than .NET then you could loop over the results and populate a collection and return the collection. I'm not 100% of the VB.NET syntax (I usually work in C#), but something like:

Dim teams as List(Of String) = new List(Of String)()

replace your "return display1" with:

teams.Add(rdr[0].ToString())

Then "return teams" when you exit the loop.

Author

Commented:
Thank you CraigWagner:

I will go off a play with this now.

It is for an iphone app...  So hopefully something simular to this will aplly when i try and build the app.

I will come back in a day or so..

Baker

Author

Commented:
Thank you, after a lot of work, i think i got it working