Solved

Return multiple rows from a web service

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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