Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Feb 21

Written by: Steve Gray
2/21/2011 3:40 PM  RssIcon

This is a short example on how to create a DataTable dynamically, assign a primary key to it, and then how to update it

You can search a data table with or without a primary key, but if you don’t use the key you’ll get back an array of rows; in this case the single row is really what we need.

This code is very useful for submitting samples to vendors, you don’t have to fake data connections.

There is no error checking, this is just template code.

Code Snippet
  1. Function GetDataTable() As DataTable
  2.     Dim oDT As New DataTable
  3.     Dim oKeys(0) As DataColumn
  4.     'create a data table to use as our input source
  5.  
  6.     'create columns
  7.     Dim oCol As DataColumn = New DataColumn("order", System.Type.GetType("System.String"))
  8.     oDT.Columns.Add(oCol)
  9.     oKeys(0) = oCol
  10.     oDT.Columns.Add(New DataColumn("salesperson", System.Type.GetType("System.String")))
  11.     oDT.Columns.Add(New DataColumn("amount", System.Type.GetType("System.Double")))
  12.     oDT.PrimaryKey = oKeys
  13.  
  14.     'create the rows
  15.     oDT.Rows.Add("ORD00100", "Bob@test.com", 1.23)
  16.     oDT.Rows.Add("ORD00102", "Bob@test.com", 3.23)
  17.     oDT.Rows.Add("ORD00104", "Sam@test.com", 2.23)
  18.     oDT.Rows.Add("ORD00105", "Sam@test.com", 4.23)
  19.  
  20.     Return oDT
  21.  
  22. End Function
  23.  
  24. Sub updateDataTable(ByVal strOrder As String, ByVal dblAmount As Double)
  25.     Dim oRow As DataRow
  26.  
  27.     '"order" is our primary key. Search the primary key for strOrder
  28.     oRow = oDT.Rows.Find(strOrder)
  29.  
  30.     'in the row found, set the Amount column
  31.     oRow("amount") = dblAmount
  32.  
  33.     'save
  34.     oDT.AcceptChanges()
  35.  
  36. End Sub

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us