Extracting data from dataset


Jun 29, 2004
Programming Experience
Yep, me again!

I'm building a library program. I've successfully extracted data from an Oracle 9i database and bound the the data to controls. Here the code that establishes my connection and 'Fills' the dataset:
Public Class Form1
Inherits System.Windows.Forms.Form

'Declare Objects

Dim connectStr As String = "Provider=MSDAORA.1; User Id=books;Password=books;Data Source=Library"

Dim con As New OleDbConnection(connectStr)

Dim objA As OleDbDataAdapter = New OleDbDataAdapter( _
"Select firstname, lastname, title, year_pub, details.isbn, details.cost, " & _
"type_of_book, cover_of_book, inout, owner, been_read, " & _
"from authors, details where authors.isbn = details.isbn " & _
"order by lastname", con)

Dim objDataSet As DataSet
Dim objDataView As DataView
Dim objCurrencyManager As CurrencyManager

Private Sub FillDataSetAndView()
'initialize a new instance of the Dataset object...
objDataSet = New DataSet

'fill with DataSet object with data...
objA.Fill(objDataSet, "details")

'set the DataView object to the DataSet object...
objDataView = New DataView(objDataSet.Tables("details"))

'set our CurrencyManager object to the DataView object
objCurrencyManager = CType(Me.BindingContext(objDataView), CurrencyManager)
End Sub

Notice the 'been_read' and 'owner' columns. They return specific values that I need to read in order to set a radio button on my window application. Depending on the values returned by the columns, I activate a radio button. How do I read the values? Should I create a DataTable?

All I really want to do is read the values into variable then use the results in an IF-ELSE statement. How do you read a dataset? I've been surfing the Net attempting to find an example. So far, zilch.

Mike Parish, Toronto, Canada
If you are going to read the data, a better solution may be to use an OleDbDataReader and its Read and GetData (GetBoolean, GetString, ...) methods instead of creating and filling a dataset. A dataReader has less overhead and is quicker.

A dataset is a container for things like dataTables, relationships, .... When you use this statement: 'objA.Fill(objDataSet, "details")' you are creating a dataTable that is contained in the dataSet objDataSet (the table is named 'details') and filling the dataTable with the data.

If you need to create a dataSet for other purposes, you can read a dataSet (actually the dataTable within the dataSet) by iterating through the Rows collection of the dataTable and the Item collection of the individual Row:
Dim i As Integer
For i = 0 To objDataSet.Tables("details").Rows.Count - 1
    Dim strTest As String = objDataSet.Tables("details").Rows(i).Item(0)

For both the Tables collection of the dataSet and the Item collection of the Row you can use either an index or a string.
Thanks. It working like a charm.

I actually tried the OleDBDataReader but I could not get it to sync with my dataset.

I'm a rookie, but I'm getting better

Mike Parish
Toronto, Canada