ASP.NET: Loop through data in a DataTable
From Wiki
Summary: An example that uses the Northwind Database to loop through data using a DataTable
- ' Declarations
- Dim MyConnection As System.Data.SQLClient.SqlConnection
- Dim MyDataAdapter As SqlClient.SqlDataAdapter
- Dim MyDataTable As New DataTable
- Dim MyDataRow As DataRow
- Dim strSQL As String = "SELECT ProductName FROM products"
- ' Connect and fill the DataTable
- MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=Northwind;uid=USERNAME;pwd=PASSWORD")
- MyConnection.Open()
- MyDataAdapter = New SqlClient.SqlDataAdapter(strSQL, MyConnection)
- MyDataAdapter.Fill(MyDataTable)
- ' Loop through DataTable
- For Each MyDataRow In MyDataTable.Rows
- Response.Write(MyDataRow.Item("ProductName"))
- Next
This Hack is part of the ASP.NET Hacks collection


