Results 1 to 12 of 12

Thread: Using a DataReader to populate a dataset

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Using a DataReader to populate a dataset

    I'm having a bit of an issue with the following code. Basically, I have an oracle command that queries a database and returns records - that part works and is located above the snippet I've posted.

    It loads the records into a datareader, and I can loop through the datareader - that works also.

    Where I'm having a problem is - I want to loop through the datareader and load records into a datatable I've created (by dragging a Datatable object onto my form). I've then linked a datagridview on my form to that datatable.

    The code below runs without error, but the datagridview doesn't populate with anything - wondering why?

    The highlighted line below shows where I'm trying to see if there are any rows in my datatable. This always seems to be zero so the issue is somewhere in here I suspect.

    Code:
    Dim OracleCommand As New OracleCommand
            OracleCommand.Connection = con
            OracleCommand.CommandText = IP_SQL
            OracleCommand.CommandType = CommandType.Text
    
            Dim DataReader As OracleDataReader = OracleCommand.ExecuteReader()
    
            If DataReader.HasRows Then
                Do While DataReader.Read()
                    Dim anyRow As DataRow = dtNRCWorking.NewRow
                    anyRow("MRN") = Mid((DataReader.Item("CR_Number")), 3, 7)
                    Dim intwow As Integer = dtNRCWorking.Rows.Count
    
                Loop
    
            End If
    
            DataGridView1.DataSource = dsNRCPicker
            DataGridView1.DataMember = "dtNRCWorking"
            DataGridView1.Refresh()
            DataReader.Close()


    Now I'm sure some of you are wondering why I'm doing this in the first place. Well - in short, I need to query data, massage it, and create a tab delimited file with it.

    The DataReader let's me query, the dataset lets me loop through and massage the records/fields that I need to massage, and the datagridview is just for testing purposes so I can have a quick look at the data I'm building.
    Last edited by The_Grudge; May 29th, 2013 at 11:42 AM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Using a DataReader to populate a dataset

    vb.net Code:
    1. If DataReader.HasRows Then
    2.             Do While DataReader.Read()  
    3.                 Dim anyRow As DataRow = dtNRCWorking.NewRow ' you want an empty datarow?
    4.  
    5.                 anyRow("MRN") = Mid((DataReader.Item("CR_Number")), 3, 7) ' now you have a datarow with only one value in the MRN column
    6. ' and now you completely ignore it by failing to add it to the table
    7.                 Dim intwow As Integer = dtNRCWorking.Rows.Count ' so this has to be 0
    8.  
    9.             Loop
    10.  
    11.         End If
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using a DataReader to populate a dataset

    use the data reader to fill the data table... thusly:

    datatable.fill(yourDataReader)

    now do what you need to do with the datatable. You don't need to loop through the reader yourself.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Using a DataReader to populate a dataset

    Quote Originally Posted by techgnome View Post
    use the data reader to fill the data table... thusly:

    datatable.fill(yourDataReader)

    now do what you need to do with the datatable. You don't need to loop through the reader yourself.

    -tg
    My purpose for looping through the datareader was so that I could check the fields in each record and modify them as needed (e.g. date formats, length). Are you suggesting I loop through the datatable instead?

    I tried your code - it says "fill is not a member of 'System.Data.DataTable".

    At this point I feel like I should mention I'm relatively new to VB.NET

    I appreciate your help.

  5. #5

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Using a DataReader to populate a dataset

    Quote Originally Posted by dunfiddlin View Post
    vb.net Code:
    1. If DataReader.HasRows Then
    2.             Do While DataReader.Read()  
    3.                 Dim anyRow As DataRow = dtNRCWorking.NewRow ' you want an empty datarow?
    4.  
    5.                 anyRow("MRN") = Mid((DataReader.Item("CR_Number")), 3, 7) ' now you have a datarow with only one value in the MRN column
    6. ' and now you completely ignore it by failing to add it to the table
    7.                 Dim intwow As Integer = dtNRCWorking.Rows.Count ' so this has to be 0
    8.  
    9.             Loop
    10.  
    11.         End If
    1) I thought I wanted an empty data row to put my new record into?
    2) That's okay for testing purposes -- I'll add more cols later
    3) Thanks. The dsNRCPicker.Tables(0).Rows.Add(anyRow) seemed to have slipped my mind.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Using a DataReader to populate a dataset

    I tried your code - it says "fill is not a member of 'System.Data.DataTable".

    At this point I feel like I should mention I'm relatively new to VB.NET
    No, it's not. Generally speaking we don't use DataReader so I think tg's got a bit confused.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using a DataReader to populate a dataset

    Only sort of... I think I forgot the adaptor bit... youradaptor.fill(yourDataTable, yourDataReader) ... I *KNOW* there is a way to fill the DT with a DR...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Using a DataReader to populate a dataset

    You were closer the first time ... dt.Load(dr)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Using a DataReader to populate a dataset

    Thanks to you both. I have this working - almost.

    vb.net Code:
    1. Dim OracleCommand As New OracleCommand
    2.         OracleCommand.Connection = con
    3.         OracleCommand.CommandText = IP_SQL
    4.         OracleCommand.CommandType = CommandType.Text
    5.  
    6.         Dim DataReader As OracleDataReader = OracleCommand.ExecuteReader()
    7.  
    8.        
    9.         If DataReader.HasRows Then
    10.             Do While DataReader.Read()
    11.                 Dim anyRow As DataRow = dtNRCWorking.NewRow
    12.                 anyRow("MRN") = Mid((DataReader.Item("CR_Number")), 3, 7)
    13.                 anyRow("VisitNum") = DataReader.Item("PCS_Visit")
    14.                 dsNRCPicker.Tables(0).Rows.Add(anyRow)
    15.                 ' Dim intwow As Integer = dtNRCWorking.Rows.Count
    16.  
    17.             Loop
    18.  
    19.         End If
    20.  
    21.         DataGridView1.DataSource = dsNRCPicker
    22.         DataGridView1.DataMember = "dtNRCWorking"
    23.         DataGridView1.Refresh()
    24.         DataReader.Close()



    It populates my datagridview, but only with the first MRN column, not the VisitNum column. I know dunfiddlin asked about my "Dim anyRow As DataRow" line of code. As I mentioned, I'm new to VB.NET, but what I thought this was doing was creating a new row within my datatable that the subsequent lines of code would populate.

    Here's what I'm trying to do - this may make things more clear and someone may have a better solution.

    I need to submit a data file to an agency on a monthly basis. They've provided me with the layout of the file (tab delimited). The data they've requested comes from a few different databases I have access to, and they've specified that the fields must be in certain formats.

    The method of my madness was this....I'm going to run a query, return the data, and loop through it so that I may
    a) validate the data
    b) format the data

    From here, I will fire the "massaged" data into a dataset that builds as more data sources are queried. I'm basically building the text file as I go and putting it into the datagridview so I can see my progress.

    So, in the above program I've queried the data with a datareader and then I want to loop through the datareader so that I can look at each field/each record and throw the massaged values into my dataset.
    Last edited by The_Grudge; May 30th, 2013 at 08:07 AM.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using a DataReader to populate a dataset

    set a breakpoint on this line:
    Dim anyRow As DataRow = dtNRCWorking.NewRow

    and then step through the code one line at a time and check your values... if the other field isn't getting set, there's either nothing in the source, or there's a problem with the types and it's not converting properly... or.... ???

    another possiblility is to write the query and data extraction in the SQL itself... then you'd only have to execute it and stuff the results into the datatable directly... that would be the way I'd do it...

    As for the Dim anyRow...
    Dim anyRow As DataRow = dtNRCWorking.NewRow

    that's fine... but this
    dsNRCPicker.Tables(0).Rows.Add(anyRow)

    should be changed to this:
    dtNRCWorking.Rows.Add(anyRow)

    just for consistency sake. And to make sure you're going against the right table.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Using a DataReader to populate a dataset

    Seems like you would load the reader data using DataTable Load method

    Of course for you the data provider would change.
    Code:
    Public Sub demo()
        Using cn As New OleDb.OleDbConnection With {.ConnectionString = "Your connection string"}
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                cmd.CommandText = "SELECT * FROM SomeTable"
                Dim dt As New DataTable
                cn.Open()
                dt.Load(cmd.ExecuteReader)
            End Using
        End Using
    End Sub
    Then cycle thru the rows as needed to inspect your data.

  12. #12

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Using a DataReader to populate a dataset

    I got it working. The data was in the datatable, but I needed to create the corresponding column in the DataGridView. Once I did that it had somewhere to put the data.

    I would have thought that binding the datagridview to the dataset would be more automatic. As fields come and go the datagridview would grow and shrink accordingly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width