loop through rows in a table

  • Thread starter Thread starter Fraggle
  • Start date Start date
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
 
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.
 
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
 
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.
 
Back
Top