Solved

Return multiple rows from a web service

Posted on 2014-03-08
5
784 Views
Last Modified: 2014-03-10
I have a VB.net web service that I need to return 1 or more rows from.

I know how to return a single value, but I'm stuck trying to return multiple rows with a single column.

The rows will be pulled from a database table, for example:

"SELECT title FROM Books" returns x rows

The service will be consumed by an iOS device so the result set needs to be in an 'open' format.
0
Comment
Question by:BrianFord
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39914811
a web service basically returns nothing but a XML ... structured according to the "contract" of what the webservice should do.
so, I am not sure what the issue is you are looking at, or the "technical" detail you need help with exactly ...
please clarify
0
 

Author Comment

by:BrianFord
ID: 39914875
OK so, in the sample function below, I am returning just a single piece of information

  <WebMethod()> _
    Public Function getStuff (ByVal pParam as string) As string

        dim tempString as string = pParam

       Return tempString

End Function

Open in new window


The pseudo code for what I want to do would be:

  <WebMethod()> _
    Public Function getStuff (ByVal pParam as string) As <whatever type this needs to be>

        select 10 rows from my database <== this bit I know how to do

       Return the 10 rows <== This is the bit I'm stuck on

End Function

Open in new window


I'm not sure if I should be returning an Array or an Object etc... XML would be best but I'm not sure how to build that XML
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 39916163
you have 2 options: you can put As DataSet  or As DataTable
and return the DataSet or the DataTable of the query accordingly.
it's as simple as that. the client consuming the webservice just has to consume the results as DataSet (or DataTable).
0
 
LVL 10

Accepted Solution

by:
Monica P earned 300 total points
ID: 39917102
Try like this


write the ELSE as you like in below ..I have given just a sample code..
Make modification as per your need..

 <WebMethod()> _
    Public Function Test(ByVal Agentid As String) As String()
        Dim a() As String
dim dt as new datatable
        Try
            dt = obj.ExecuteData("SELECT title FROM Books")
            If dt.Rows.Count > 0 Then
                Dim drow As DataRow
                For j = 0 To dt.Rows.Count - 1
                    ReDim Preserve a(dt.Rows.Count)
                    Try
                        drow = dt.Rows(j)
                        a(j) = drow("title").ToString
                    Catch ex As Exception
                      
                    End Try
                Next

                Return a

            End If

        Catch ex As Exception
            ErrLog.WriteLog("Error : failure " & Space(1) & ex.Message.ToString)        
        End Try
   End Function

Open in new window


Fill the datatable :

Public Function ExecuteData(ByVal fetch As String) As DataTable

        Dim dt As DataTable
        Dim ds As New DataSet
        Dim da As SqlDataAdapter
        Try
            Connection()
            OpenConnection()
            Dim strcmd As SqlCommand
            strcmd = New SqlCommand(fetch, cnn)
            da = New SqlDataAdapter(strcmd)
            ds = New DataSet
            da.Fill(ds)
            dt = ds.Tables(0)
        Catch ex As SqlException
            Throw ex

        Finally
            CloseConnection()
        End Try

        Return dt
    End Function

Open in new window

0
 

Author Closing Comment

by:BrianFord
ID: 39917360
Thank you all
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now