Return Dataset from Web Service

Last post 11-07-2009 12:48 PM by TATWORTH. 2 replies.

Sort Posts:

  • Return Dataset from Web Service

    11-02-2009, 5:33 PM
    • Member
      point Member
    • sydkenber
    • Member since 10-28-2009, 5:14 PM
    • Posts 2

    Hello all,

    I think this should be a simple issue but I am having nothing but trouble.  My code currently passes a system.object containing an array of paramaters to a web service.  The WS then takes thes parameters and calls a stored procedure and returns a dataset to the calling web form.  I have converted a former class into a web service with no luck.  If I put the .vb file in the app_code folder it works fine, if I add it as a web reference from another location I get an error stating that there was an error creating the XML file. 

    Here is the calling code:

    Dim clsGN as new Service
    Dim ds as new DataSet
    Dim arParams(0) as object

    ds = clsGN.GetData("spName", arParms)

    WS Code:

     <WebMethod(MessageName:="GetData", Description:="gets dataset with parameters")> _
        Public Function GetData(ByVal SPName As String, ByVal SPVals As Object) As DataSet
    
            Dim oDS As New DataSet
            Dim oDSVals As New DataSet
            Dim adapter As SqlDataAdapter
            Dim conn As SqlConnection
            Dim comm As SqlCommand
    
    
            'Get the paramater value types from the stored procedure
            oDSVals = GetSPValueTypes(SPName)
    
            'Initialize connection
            conn = New SqlConnection(connectionString)
    
            'Create Command
            comm = New SqlCommand(SPName, conn)
            comm.CommandType = CommandType.StoredProcedure
    
            Dim x As Integer = 0
    
            For x = 0 To SPVals.GetUpperBound(0)
    
                Dim sType As New SqlDbType
                Dim sParamName As String = String.Empty
                'Get the SQL parameter data type from oDSVals to attach to the command parameters
                sType = AssignSQLParameterType(oDSVals.Tables(0).Rows(x).Item("data_type"))
                sParamName = oDSVals.Tables(0).Rows(x).Item("parameter_name")
    
                comm.Parameters.Add(sParamName, sType)
                comm.Parameters(sParamName).Value = SPVals(x)
            Next
    
            'Create adapter
            adapter = New SqlDataAdapter(comm)
    
            'Fill the dataset
            adapter.Fill(oDS)
    
            'Close the connection
            conn.Close()
            conn.Dispose()
            comm.Dispose()
            oDSVals.Dispose()
    
            Return oDS
    
        End Function 

    Thanks for your help.

     

  • Re: Return Dataset from Web Service

    11-02-2009, 6:01 PM
    Answer
    • Participant
      899 point Participant
    • N_EvilScott
    • Member since 07-22-2008, 11:28 PM
    • Oregon
    • Posts 203

    You will need a custom converter / parser to serialize the dataset, it is not a normal function of a web service. There are other options such as returning straight up XML, but you won't be able to return an actual DataSet without some extra coding involved. 

    Scott was here...
    http://www.orionmedicalservices.com
    http://www.seideleyecare.com
    http://www.freytagdesigns.com
  • Re: Return Dataset from Web Service

    11-07-2009, 12:48 PM
    Answer
    • All-Star
      63,002 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,309
    • TrustedFriends-MVPs

    A dataset can be returned by a Web Service e.g.

        /// <summary>
        /// Gets the event log dataset.
        /// </summary>
        /// <returns>Dataset of Event Log Entries</returns>
        [WebMethod(Description = "Gets the event log dataset")]
        public DataSet GetEventLogDataset()
        {
          return CommonData.GetEventLogDataSet;
        }

    This is from CommonService.asmx in the CommonWebService project in the CommonData solution at http://commondata.codeplex.com/

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (3 items)