Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

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: 5.0/5 (1 vote cast)