Loop through a dataset to get values, vb .net

E

edpdc

VB .NET
I would like to know how to loop through each row in a data set and
get a value from a colum in the data set. My data set has multiple
columns. I know I have to use the datarow class but I'm not sure how
to do this. I would like to use a for each loop.
Any help would be appreciated.
 
S

Shiva

Hi,
Actually, datasets do not contain datarows or columns. Only datatables have
them.

Here is a code snippet that does the looping:

Dim dr As DataRow
Dim ds As Dataset
Dim dt As DataTable
<code to fill the dataset>
dt = ds.Tables(0)
For Each dr In dt.Rows
Console.WriteLine (dr("ColName"))
Next
ds.Dispose()

Hope this helps.

VB .NET
I would like to know how to loop through each row in a data set and
get a value from a colum in the data set. My data set has multiple
columns. I know I have to use the datarow class but I'm not sure how
to do this. I would like to use a for each loop.
Any help would be appreciated.
 
N

Nick Malik

If you are going to make a point of asking a question in the csharp
newsgroup, you should expect to get your answer in C#.

DataTable tbl = myDataSet.Tables[0];
for (int i = 0; i < tbl.Rows.Count; i++)
{
DataRow myRow = tbl.Rows;
string MyValue = myRow["datafieldname"];
}

This can be written more efficiently... I drew out the data types for
clarity.
--- N
 
T

The Last Gunslinger

edpdc said:
VB .NET
I would like to know how to loop through each row in a data set and
get a value from a colum in the data set. My data set has multiple
columns. I know I have to use the datarow class but I'm not sure how
to do this. I would like to use a for each loop.
Any help would be appreciated.

Since this is a c# ng, code is in c# :)

DataSet ds = new DataSet();
foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["ColName"].ToString());
}

HTH
JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top