Instead of popluating a datareader popluate a dataset. Once the data is in a dataset you can loop through it.
You can fill your dataset like this:
Dim cnn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("YourString").ConnectionString)
cnn.Open()
Dim sqlAdp As New Data.SqlClient.SqlDataAdapter
Dim cmd As New Data.SqlClient.SqlCommand
Dim ds As New Data.DataSet
cmd.Connection = cnn
cmd.CommandText = "Select * From SomeTable"
cmd.Parameters.Clear()
sqlAdp.SelectCommand = cmd
sqlAdp.Fill(ds)
cnn.Close()
Now you can loop through your dataset like this:
For Each r In ds.Tables(0).Rows
For Each c In ds.Tables(0).Columns
Response.Write(ds.Tables(0).Rows(r)(c).ToString & " ")
Next
Response.Write("<br />")
Next
Loganix77
Participant
1351 Points
412 Posts
Re: Looping thorough a query and storing the row values in a Dictonary or someother object
May 04, 2012 09:23 PM|LINK
Instead of popluating a datareader popluate a dataset. Once the data is in a dataset you can loop through it.
You can fill your dataset like this:
Dim cnn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("YourString").ConnectionString) cnn.Open() Dim sqlAdp As New Data.SqlClient.SqlDataAdapter Dim cmd As New Data.SqlClient.SqlCommand Dim ds As New Data.DataSet cmd.Connection = cnn cmd.CommandText = "Select * From SomeTable" cmd.Parameters.Clear() sqlAdp.SelectCommand = cmd sqlAdp.Fill(ds) cnn.Close()Now you can loop through your dataset like this:
For Each r In ds.Tables(0).Rows For Each c In ds.Tables(0).Columns Response.Write(ds.Tables(0).Rows(r)(c).ToString & " ") Next Response.Write("<br />") Next