Parag’s Den

Dedicated to my adored friends…..

Asp.Net (How to loop through a DataTable?)

Posted by paragdongre on November 6, 2007


The following example tells you how to loop through a DataTable. We can relate the DataTable to the RecordSet in Classic ASP (Active Server Pages).

Dim adapter As SqlDataAdapter
Dim ds As DataSet
Dim ctr As Integer

Try
     adapter = New SqlDataAdapter(“YourSQLStatement”, “YourConnectionString”)
     ds = New DataSet()
     adapter.Fill(ds, “myTable”)

     If ds.Tables(0).Rows.Count > 0 Then
          For ctr = 0 To ds.Tables(“entries”).Rows.Count – 1
               Response.Write(ds.Tables(0).Rows(ctr).Item(“ColumnName”))
          Next
     End If

Catch exc As SqlException
     Response.Write(“SQL Error Occured: ” & exc.ToString)

Catch exc As Exception
     Response.Write(“Error Occured: ” & exc.ToString)

Finally
     adapter = Nothing
     ds = Nothing

End Try

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: