Merge multiple datasets into one

  • Thread starter Thread starter John Cobb
  • Start date Start date
J

John Cobb

Front end is passing in a string array of "types" of items to be retrieved.
I loop through array setting parameter and executing stored procedure to
populate a dataset but have had no luck combining all the rows into one
table. I've tried several variations of the below code and receive an "row
already belongs to a table." error. Help!

'Loop through types
For intLoop = 0 To Type.GetUpperBound(0)

' Execute query
spParms(0) = New SqlParameter("@Type", Type(intLoop))
spParms(0).Direction = ParameterDirection.Input

dsItems = SqlHelper.ExecuteDataset("dev.dbo.GetItemList",
spParms)

' add rows to dataset which will be returned
With dsItems.Tables(0)

'copy the table structure to the empty dataset which
will be returned
If dsReturn.Tables.Count = 0 Then
dsReturn.Tables.Add(dsItems.Tables(0).Clone)
End If

If .Rows.Count > 0 Then
For intX = 0 To .Rows.Count - 1
Dim myRow As DataRow = .Rows(intX)
.Rows(intX).Delete()
dsReturn.Tables(0).Rows.Add(myRow)
Next
End If
End With
Next

Thanks in advance for any help.
John
 
If you use the New keyword then you have to remove the assignment portion of
the line or you receive and "end of statement expected." error.
 
Hello John,
If you use the New keyword then you have to remove the assignment
portion of the line or you receive and "end of statement expected."
error.

Apologies... My VB.NET is horrendous. ;)
 
Found an answer although I don't know if it's the best answer.

dsReturn.Tables(0).LoadDataRow(dsItems.Tables(0).Rows(intX).ItemArray, True)
 
Back
Top