By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Read data from database table row by row

P: n/a
I would like to read data from a database table row by row and execute some codes based on the data retrieved from each row. For example, there are 4 fields on a table called Image_Tbl. Those fields are Image ID, Image Path Name, X-Coordinate, and Y-Coordinate. Data stored on the table will be

Image ID Image Path Name X-Coordinate Y-Coordinat
ImgBtnLight image\light.gif 192px 92p
ImgBtnAC image\ac.gif 256px 152p

How should I make a loop saying before end of the database table records and for each row of data, execute the some codes. Thank you for your time

Thanks
Ester
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Ester,

When you are not posting to a language group it is better to tell which
language you use.

You can do this in VB
\\\
for i as integer = 0 to ds.tables(0).rowcount - 1
myfirsfield = ds.tables(0).rows(i)(0) 'the 0 can also be a name as "myX"
in both situations
'do somestuff
next
///
I hope this gives some ideas?

Cor
Jul 21 '05 #2

P: n/a
Ester <an*******@discussions.microsoft.com> wrote:
I would like to read data from a database table row by row and
execute some codes based on the data retrieved from each row. For
example, there are 4 fields on a table called Image_Tbl. Those fields
are Image ID, Image Path Name, X-Coordinate, and Y-Coordinate. Data
stored on the table will be:

Image ID Image Path Name X-Coordinate Y-Coordinate
ImgBtnLight image\light.gif 192px 92px
ImgBtnAC image\ac.gif 256px 152px

How should I make a loop saying before end of the database table
records and for each row of data, execute the some codes. Thank you
for your time.


Assuming you've already loaded the table into a DataTable (eg using
DataAdapter.Fill)...

In C#:

foreach (DataRow row in table.Rows)
{
// Do something with row
}

In VB.NET:

Dim row as DataRow
For Each row in table.Rows
' Do something with row
Next

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

P: n/a
Just to make the 2 versions equal

In C#:
foreach (DataRow row in table.Rows)
{
// Do something with row
}
In VB.net 2003
For Each row as DataRow in table.Rows
' Do something with row
Next

Jul 21 '05 #4

P: n/a
Cor Ligthert <no**********@planet.nl> wrote:
Just to make the 2 versions equal

In C#:
foreach (DataRow row in table.Rows)
{
// Do something with row
}
In VB.net 2003
For Each row as DataRow in table.Rows
' Do something with row
Next


Cheers for that - for some reason I thought you couldn't declare a new
variable as the For Each loop variable. I'm sure I tried it the other
day and couldn't get it to work. Your sample works fine, of course,
which I'm very glad of...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.