Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Loop through data in a DataTable

From Wiki

Jump to: navigation, search


Summary: An example that uses the Northwind Database to loop through data using a DataTable

  1. ' Declarations  
  2. Dim MyConnection As System.Data.SQLClient.SqlConnection
  3. Dim MyDataAdapter As SqlClient.SqlDataAdapter
  4. Dim MyDataTable As New DataTable
  5. Dim MyDataRow As DataRow
  6. Dim strSQL As String = "SELECT ProductName FROM products"
  7.  
  8. ' Connect and fill the DataTable  
  9. MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=Northwind;uid=USERNAME;pwd=PASSWORD")  
  10. MyConnection.Open()  
  11. MyDataAdapter = New SqlClient.SqlDataAdapter(strSQL, MyConnection)  
  12. MyDataAdapter.Fill(MyDataTable)  
  13.  
  14. ' Loop through DataTable  
  15. For Each MyDataRow In MyDataTable.Rows  
  16.     Response.Write(MyDataRow.Item("ProductName"))  
  17. Next


This Hack is part of the ASP.NET Hacks collection

448 Rating: 2.8/5 (4 votes cast)