How to Use Two Datasets in VB

104 12
    • 1). Launch Microsoft Visual Studio, click "New Project" from the left pane of your computer screen, and expand "Visual Basic" below "Installed Templates." Click "Windows" and double-click "Console Application" from the center of the dialog window to create a new console project.

    • 2). Copy and paste the following code to create the first "DataSet," a table, two columns, and three rows:

      Dim dataSet As New DataSet("dataSet")

      Dim table As New DataTable("Items")

      Dim idColumn As New DataColumn("id", Type.GetType("System.Int32"))

      idColumn.AutoIncrement = True

      Dim itemColumn As New DataColumn("Item", Type.GetType("System.Int32"))

      Dim row As DataRow

    • 3). Add the following line of code to create the second "DataSet:"

      Dim changeDataSet As DataSet

    • 4). Add coulumns to table and add the table to the "DataSet:"

      table.Columns.Add(idColumn)

      table.Columns.Add(itemColumn)

      dataSet.Tables.Add(table)

    • 5). Add ten rows of data to the table and accept the changes:

      Dim i As Integer

      For i = 0 To 9

      row = table.NewRow()

      row("Item") = i

      table.Rows.Add(row)

      Next i

      dataSet.AcceptChanges()

    • 6). Print "DataSet" values to the Console window:

      PrintValues(dataSet, "Original values")

    • 7). Modify the table by adding new values to the first two rows, adding a new row, and adding an error to one of the rows:

      table.Rows(0)("Item") = 50

      table.Rows(1)("Item") = 111

      row = table.NewRow()

      row("Item") = 74

      table.Rows.Add(row)

      table.Rows(1).RowError = "over 100"

      PrintValues(dataSet, "Modified and New Values")

    • 8). Check if the table has changes and if it does then merge changes back to the first "DataSet" and print the results:

      If dataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) _

      And dataSet.HasErrors Then

      changeDataSet = dataSet.GetChanges(DataRowState.Modified _

      Or DataRowState.Added)

      PrintValues(changeDataSet, "Subset values")

      Dim changeTable As DataTable

      For Each changeTable In changeDataSet.Tables

      If changeTable.HasErrors Then

      Dim changeRow As DataRow

      For Each changeRow In changeTable.Rows

      If CInt(changeRow("Item", _

      DataRowVersion.Current)) > 100 Then

      changeRow.RejectChanges()

      changeRow.ClearErrors()

      End If

      Next changeRow

      End If

      Next changeTable

      PrintValues(changeDataSet, "Reconciled subset values")

      dataSet.Merge(changeDataSet)

      PrintValues(dataSet, "Merged Values")

      Console.ReadLine()

      End If

      End Sub

    • 9). Add the following sub procedure to print "DataSet" values to the Console window:

      Private Sub PrintValues(ByVal dataSet As DataSet, ByVal label As String)

      Console.WriteLine(ControlChars.Cr & label)

      Dim table As DataTable

      For Each table In dataSet.Tables

      Console.WriteLine("TableName: " & table.TableName)

      Dim row As DataRow

      For Each row In table.Rows

      Dim column As DataColumn

      For Each column In table.Columns

      Console.Write(ControlChars.Tab & " " _

      & row(column).ToString())

      Next column

      Console.WriteLine()

      Next row

      Next table

      End Sub

    • 10

      Press "F5" to run the program.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.