Solved

Return multiple rows from a web service

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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