Saturday, August 07, 2004

Question: I am trying to understand the dataset and how I can use it. It looks like a really powerful feature but it has an incredible amount of objects. It seems to me that I should be able to do simple things like create and edit values. But for the life of me I can’t seem to figure out how it works. Do you have some simple example that show how you can create a dataset and then update the individual rows?

 

Answer: Sure – here are some examples.

 

  1. The following creates a dataset that we can use in the later examples.

' create the item table

Dim tblitem As DataTable = New DataTable("item")

Dim itemid As DataColumn = New DataColumn("itemno", _ Type.GetType("System.Int32"))

Dim colititle As DataColumn = New DataColumn("title", _ Type.GetType("System.String"))

Dim colipubdate As DataColumn = New DataColumn("pubdate", _ Type.GetType("System.String"))

Dim colidesc As DataColumn = New DataColumn("description", _ Type.GetType("System.String"))

Dim colilink As DataColumn = New DataColumn("link", _ Type.GetType("System.String"))

‘ create the columns

tblitem.Columns.Add(itemid)

tblitem.Columns.Add(colititle)

tblitem.Columns.Add(colipubdate)

tblitem.Columns.Add(colidesc)

tblitem.Columns.Add(colilink)

‘ add it to the dataset

ds.Tables.Add(tblitem)

 

  1. Create a datatable from the dataset and insert a datarow.

Dim dt As DataTable = ds.Tables("item")

' get the datarow

Dim dtrow As DataRow

dtrow = dt.NewRow()

dtrow("itemno") = 10

dtrow("link") = "http://www.msn.com"

dt.Rows.Add(dtrow)

 

  1. Select the new row and edit the value

Dim dr As DataRow() = dt.Select("itemno=10")

If dr.Length > 0 Then

    dr(0).Item("link") = "http://www.test.com"

End If

dt.AcceptChanges()


11:00:34 PM