How to read a DataSet?

D

D. Shane Fowlkes

How can I loop through the rows of a DataSet and send the "fields" to the
Function as I've attempted to do in my code? Error is at "While
dsLineItems.Read()" . I'm trying to construct a Sub which builds a DataSet
and loops through the records or "rows" of the DataSet and call a Function
for each row found. The Function will insert the data into another table.
(Essentially what I'm doing is pulling records from one Temp working table
and inserting them into another indentical table). About a week ago, I asked
about how to read data from one table and insert it into another. I was
considering a multi-dim array but someone suggested I just use a DataSet. It
seemed like a simpler approach so that's why I'm using a DS to store data
from one table to be inserted into another.

Thanks!



Sub ReadTempLineItems(intNewReqID As Integer)


Dim objConnection As SqlConnection
Dim adpData As SqlDataAdapter
Dim dsLineItems As DataSet

Dim strConnectString As String
Dim strSQL As String

strConnectString =
System.Configuration.ConfigurationSettings.AppSettings("SqlConnection")
strSQL = "SELECT * FROM POItemsTemp WHERE PurchaseOrderID = " &
Session("intReqID")

dsLineItems = New DataSet()
objConnection = New SqlConnection(strConnectString)
adpData = New SqlDataAdapter(strSQL, objConnection)
adpData.Fill(dsLineItems, "LineItems")

While dsLineItems.Read() <---- Error
InsertLineItems(intNewReqID, dsLineItems("BudgetID"),
dsLineItems("AllocAmt"), dsLineItems("Description"),
dsLineItems("Quantity"), dsLineItems("UOM"), dsLineItems("UnitPrice"))
End While

drLineItems.Close()
objConnection.Close()

End Sub
 
S

Scott Allen

Hi Shane:

One thing to be careful about is that a DataSet doesn't contain rows but
contains a collection of DataTable objects and the DataTable objects contain
rows.

Something along the lines of:

Dim dsLineItems As DataSet = New DataSet()

' fill dsLineItems...

Dim row As DataRow
For Each row in dsLineItems.Tables(0).Rows
' do work, i.e. row("Quantity") = x * y
Next

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top