function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
contactSalescontactSales 

Create datatabe / dataset from the results of QueryResult

Hi,

  I am using Enterprise WSDL with VB.Net 2.0. I Want to return ADO.NET datatabe / dataset  based on the below code:-

 

Dim qr As QueryResult = Nothing

qr = binding.query("select id, Website, Name from Account")

If qr.size > 0 Then

Dim account As Account = CType(qr.records(0), Account)

Dim Id As String = account.Id

Dim Website As String = account.Website

 End If

  • So, I would like to return all the rows of account QueryResult  as a datatable ? What would be the best way? 
  • Do you provide any CreateDataTable methods in the webservice ?

Regards,

werewolfwerewolf
Sounds like you're trying to do the same stuff this guy was trying to do:

http://forums.sforce.com/sforce/board/message?board.id=NET_development&message.id=5942#M5942
jeremyfrey1jeremyfrey1
There's nothing native that I know of that'll let you convert an array of salesforce objects to a dataset or datatable.

We started going down this path of trying to convert any SOQL to a dataset with nested datatables, but abandoned the idea early on.  I believe you'll get cleaner, more maintainable code if you stick to working with collections of objects.  e.g.:

Code:
Dim qr As QueryResult = Nothing
qr = binding.query("select id, Website, Name from Account")
Dim accountList as New List(Of Account)

If qr.size > 0 Then
   for each r as Object in qr.records
       Dim account As Account = CType(r, Account)
       accountList.Add(account)
   next
End If
return accountList ' do something with accountList in another method

(Apologies if my syntax is slightly off, I haven't worked with the Enterprise WSDL)

Unless you're using a pre-2.0 version of .NET, you'll find that you can bind to such collections of objects just as easily through ObjectDataSources as you would be able to a dataset / datatable.

Michael.WatkinsMichael.Watkins
            Dim oProxy As SforceService = sfUtilities.GetProxy()
            Dim oDataSet As New DataSet
            Dim oResult As New sforce.QueryResult

            oResult = oProxy.query("SELECT ID, ContactId, CaseNumber, Subject, Status FROM Case " & sWhere)

            If Not oResult.records Is Nothing AndAlso oResult.records.Length > 0 Then
                Dim oCase As sforce.Case
                Dim oTable As New System.Data.DataTable

                '   add columns
                oTable.Columns.Add("ID")
                oTable.Columns.Add("ContactID")
                oTable.Columns.Add("CaseNumber")
                oTable.Columns.Add("Subject")
                oTable.Columns.Add("Status")
                oDataSet.Tables.Add(oTable)

                '   add data
                For Each oCase In oResult.records
                    Dim oDataRow As DataRow = oDataSet.Tables(0).NewRow
                    oDataRow.Item(0) = oCase.Id
                    oDataRow.Item(1) = oCase.ContactId
                    oDataRow.Item(2) = oCase.CaseNumber
                    oDataRow.Item(3) = oCase.Subject
                    oDataRow.Item(4) = oCase.Status
                    oDataSet.Tables(0).Rows.Add(oDataRow)
                Next
            End If

            Return oDataSet