Solved

Return multiple rows from a web service

Posted on 2014-03-08
5
948 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

627 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