Navigating a dataset

  • Thread starter Thread starter Roy Lawson
  • Start date Start date
R

Roy Lawson

I am having no problems connecting to a DB, creating a DataAdapter,
and creating a dataset...and connecting to the data. Using the
builtin data objects to do all this.

My only problem now is navigating through the data. I can get the
data into a datagrid without any problems, but I want the data to show
up in textboxes and use some sort of move next, move previous, move
last, etc (like in VB6) command to navigate the data (using
buttonX_click). I have been using the learnvisualstudio.net videos
and they are great but anyone have a link or tutorial to get me going
on this specific task?

(Learning to crawl again)

Roy Lawson
(e-mail address removed)
 
Roy,



maybe it helps you if you imagine the following:

The DataSet object contains a collection of zero or more DataTable objects, each of which is an in-memory representation of a single table. The structure of a particular DataTable is defined by the DataColumns collection, which enumerates the columns in a particular table, and the Constraint collection, which enumerates any constraints on the table. Together, these two collections make up the table schema. A DataTable also contains a DataRows collection, which contains the actual data in the DataSet.

The DataSet contains a DataRelations collection. A DataRelation object allows you to create associations between rows in one table and rows in another table. The DataRelations collection enumerates a set of DataRelation objects that define the relationships between tables in the DataSet. For example, consider a DataSet that contains two related tables: an Employees table and a Projects table. In the Employees table, each employee is represented only once and is identified by a unique EmployeeID field. In the Projects table, an employee in charge of a project is identified by the EmployeeID field, but can appear more than once if that employee is in charge of multiple projects. This is an example of a one-to-many relationship; you would use a DataRelation object to define this relationship.

Excerpt from the book "MCAD/MCSD Training Kit-Developing Windows-Based Applications with Microsoft Visual Basic .NET and Microsoft Visual C# ..NET" which I can truly recommend.



If you for example have a table "myTable" in a DataSet "myDataSet" and you want to bind the field "ID" of the first row of the table to a TextBox named TextBox1 do the following:



TextBox1.Text = myDataSet.Tables("myTable").Rows(0).Item("ID").ToString



I hope this gives you the general idea. Always visualize the collections in the ADO.NET model.



Best regards

Daniel Walzenbach
 
For what I am doing, there are no constraints on the table
(single table only) so there is only 1 DataTable object in
my DataSet. I set it up this way (1 table w/o relations)
just so I could get it going (crawl before walking).

But, your explanation was very helpful and I think with
that I can figure out programaticly how to proceed. I
suspect I need to focus on the DataColumns collection when
it comes to navigating the DataTable within my DataSet.

My code is on my laptop ... I will submit an update to
this post after I figure it out so that others can learn
to walk with me :-)

Thanks!

-Roy

-----Original Message-----
Roy,



maybe it helps you if you imagine the following:

The DataSet object contains a collection of zero or more
DataTable objects, each of which is an in-memory
representation of a single table. The structure of a
particular DataTable is defined by the DataColumns
collection, which enumerates the columns in a particular
table, and the Constraint collection, which enumerates any
constraints on the table. Together, these two collections
make up the table schema. A DataTable also contains a
DataRows collection, which contains the actual data in the
DataSet.
The DataSet contains a DataRelations collection. A
DataRelation object allows you to create associations
between rows in one table and rows in another table. The
DataRelations collection enumerates a set of DataRelation
objects that define the relationships between tables in
the DataSet. For example, consider a DataSet that contains
two related tables: an Employees table and a Projects
table. In the Employees table, each employee is
represented only once and is identified by a unique
EmployeeID field. In the Projects table, an employee in
charge of a project is identified by the EmployeeID field,
but can appear more than once if that employee is in
charge of multiple projects. This is an example of a one-
to-many relationship; you would use a DataRelation object
to define this relationship.
Excerpt from the book "MCAD/MCSD Training Kit-Developing
Windows-Based Applications with Microsoft Visual
Basic .NET and Microsoft Visual C# ..NET" which I can
truly recommend.
If you for example have a table "myTable" in a
DataSet "myDataSet" and you want to bind the field "ID" of
the first row of the table to a TextBox named TextBox1 do
the following:
TextBox1.Text = myDataSet.Tables("myTable").Rows(0).Item ("ID").ToString



I hope this gives you the general idea. Always visualize
the collections in the ADO.NET model.
 
Roy,



what you have to do is to focus on the DataRow collection, as a (single) DataColumn represents the Type of Data which is stored in your Table. I'm sure you won't be surprised when I tell you that a DataRow itself consists of a collection of DataItems which represent the actual values in a table.



Imagine the following:



ID | Name | Comment

----------------------------------

1 | Smith | Nice guy!

2 | Wilson | Not so nice.

3 | Lawson | Learns ADO.NET.

4 | Miller | Somebody else.



Here is the code to create this table by your own and print it to the console. To execute the code create a new Console Project, delete all the code in it and copy all the code below into it.



' BEGIN OF CODE *****************************************************



Public Class ADODotNETExample



Public Shared Sub main()



' Create a Table

Dim myTable As New System.Data.DataTable

' Create a Column

Dim myColumn As System.Data.DataColumn



' set values for a column

myColumn = New System.Data.DataColumn

myColumn.ColumnName = "ID"

myColumn.DataType = System.Type.GetType("System.Int32")

' add Column to Table

myTable.Columns.Add(myColumn)



myColumn = New System.Data.DataColumn

myColumn.ColumnName = "Name"

myColumn.DataType = System.Type.GetType("System.String")

' add Column to Table

myTable.Columns.Add(myColumn)



myColumn = New System.Data.DataColumn

myColumn.ColumnName = "Description"

myColumn.DataType = System.Type.GetType("System.String")

' add Column to Table

myTable.Columns.Add(myColumn)



' Now you have a empty Table (or to be more exactly

' the definition of a table without any rows)



' Create a DataRow. Remember that you don't instanciate

' a DataRow Object (by using the new() operator)

Dim myDataRow As System.Data.DataRow



' get a DataRow from the Table(description) you just created

myDataRow = myTable.NewRow

' and fill it with Life

myDataRow.Item(0) = 1

myDataRow.Item(1) = "Smith"

myDataRow.Item(2) = "Nice guy!"

' now add the DataRow to the DataTable

myTable.Rows.Add(myDataRow)



' and again for the next row

' you can also address the items by name as the

' item property is overloaded

' get a DataRow from the Table(description) you just created

myDataRow = myTable.NewRow

myDataRow.Item("ID") = 2

myDataRow.Item("Name") = "Wilson"

myDataRow.Item("Description") = "Not so nice."

' now add the DataRow to the DataTable

myTable.Rows.Add(myDataRow)



' and again and again...

' get a DataRow from the Table(description) you just created

myDataRow = myTable.NewRow

myDataRow.Item("ID") = 3

myDataRow.Item("Name") = "Lawson"

myDataRow.Item("Description") = "Learns ADO.NET."

' now add the DataRow to the DataTable

myTable.Rows.Add(myDataRow)



' get a DataRow from the Table(description) you just created

myDataRow = myTable.NewRow

myDataRow.Item("ID") = 4

myDataRow.Item("Name") = "Miller"

myDataRow.Item("Description") = "Somebody else."

' now add the DataRow to the DataTable

myTable.Rows.Add(myDataRow)





Console.WriteLine("Content of myTable")

Console.WriteLine()

Console.WriteLine()



' write content of myTable to console



' first goes the Header

For j As System.Int32 = 0 To myTable.Columns.Count - 1

Console.Write(myTable.Columns(j).ColumnName & ControlChars.Tab)

Next



' new line

Console.WriteLine()



' draw horizontal line

For j As System.Int32 = 0 To 50

Console.Write("-")

Next



Console.WriteLine()



' now print the content of the table

For i As System.Int32 = 0 To myTable.Rows.Count - 1

For j As System.Int32 = 0 To myTable.Columns.Count - 1

Console.Write(myTable.Rows(i).Item(j).ToString & ControlChars.Tab)

Next

Console.WriteLine(ControlChars.Tab & " -> Row : " & i.ToString)

Next



' wait for user to press return

Console.ReadLine()



End Sub



End Class



' END OF CODE *******************************************************



Hope this gives you the general idea.

Greetings from Germany.



Daniel Walzenbach
 
Keep on walking ;-)


Roy Lawson said:
For what I am doing, there are no constraints on the table
(single table only) so there is only 1 DataTable object in
my DataSet. I set it up this way (1 table w/o relations)
just so I could get it going (crawl before walking).

But, your explanation was very helpful and I think with
that I can figure out programaticly how to proceed. I
suspect I need to focus on the DataColumns collection when
it comes to navigating the DataTable within my DataSet.

My code is on my laptop ... I will submit an update to
this post after I figure it out so that others can learn
to walk with me :-)

Thanks!

-Roy


DataTable objects, each of which is an in-memory
representation of a single table. The structure of a
particular DataTable is defined by the DataColumns
collection, which enumerates the columns in a particular
table, and the Constraint collection, which enumerates any
constraints on the table. Together, these two collections
make up the table schema. A DataTable also contains a
DataRows collection, which contains the actual data in the
DataSet.
DataRelation object allows you to create associations
between rows in one table and rows in another table. The
DataRelations collection enumerates a set of DataRelation
objects that define the relationships between tables in
the DataSet. For example, consider a DataSet that contains
two related tables: an Employees table and a Projects
table. In the Employees table, each employee is
represented only once and is identified by a unique
EmployeeID field. In the Projects table, an employee in
charge of a project is identified by the EmployeeID field,
but can appear more than once if that employee is in
charge of multiple projects. This is an example of a one-
to-many relationship; you would use a DataRelation object
to define this relationship.
Windows-Based Applications with Microsoft Visual
Basic .NET and Microsoft Visual C# ..NET" which I can
truly recommend.
DataSet "myDataSet" and you want to bind the field "ID" of
the first row of the table to a TextBox named TextBox1 do
the following:
the collections in the ADO.NET model.
 
Back
Top