How to read a DataSet?

Discussion in 'ASP .Net' started by D. Shane Fowlkes, Jan 20, 2005.

  1. 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
     
    D. Shane Fowlkes, Jan 20, 2005
    #1
    1. Advertising

  2. D. Shane Fowlkes

    Scott Allen Guest

    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,

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    > 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
    >
     
    Scott Allen, Jan 20, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    1
    Views:
    1,818
    Chris Taylor
    Oct 31, 2003
  2. JimO

    DataSet and dataSet

    JimO, Mar 1, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    534
    Mary Chipman [MSFT]
    Mar 8, 2006
  3. Joseph D. DeJohn

    DataSet to DataSet

    Joseph D. DeJohn, Aug 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    408
  4. Nedu N
    Replies:
    2
    Views:
    2,173
  5. William Gower
    Replies:
    2
    Views:
    4,159
    Brian Baker [MVP]
    Jul 26, 2004
Loading...

Share This Page