loop through rows in a table

F

Fraggle

dim tblrow as System.Data.DataRow
dim i as integer
i = 0
for each tblrow in datasrc.tables(0)
if tblrow.item("Answer").toupper = "OTHER"
i+=1
end if
Next

This seems clear enough to me. Look at all the "Answers" in a table,
if you find "otHer" then increment 'i'

however the great ASP.framework (who we all serve) says
"Compiler Error Message: BC32023: Expression is of type
'System.Data.DataTable', which is not a collection type."

which may well be true.

Tell me how to do this properly please!

Cheers as ever
Fragg
 
B

Bill Priess

Try this... it might work better..

Dim dt as new DataTable = datasrc.Tables(0)
Dim row as DataRow
Dim i as Integer = 0

For Each row in dt.Rows
If cStr(row("Answer")).ToUpper = "OTHER" then
i += 1
End If
Next

HTH,

Bill P.
 
F

Fraggle

Bill Priess said:
Try this... it might work better..

Dim dt as new DataTable = datasrc.Tables(0)
Dim row as DataRow
Dim i as Integer = 0

For Each row in dt.Rows
If cStr(row("Answer")).ToUpper = "OTHER" then
i += 1
End If
Next

HTH,

Bill P.
It did indeed, I had to put this line in instead

Dim dt as DataTable = datasrc.Tables(0)

ie take the 'new' out, but then it works fine.

I am a bit unsure on why what you did makes the difference! but cheers

Fragg
 
P

Pat Alessi

It works because he is referenceing dt.rows (the rows collection of
the datatable), but you were just referencing the table itself
(datasrc.tables(0)). You have to refer to the rows collection, for
that is what you want to iterate through.
 

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

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top