Solved

Return multiple rows from a web service

Posted on 2014-03-08
5
846 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 143

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 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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