For Each Loop in ASP.NET 2.0

D

Dot Net Daddy

Hello,

I want to create label and checkbox for every member in the database
table.

How can I do that?

I tried:

Dim x as integer

For Each x In ds.Tables(0).Rows.Count
.... code here
Next


But it didnt work, giving an error:

Error 1 Expression is of type 'Integer', which is not a collection
type.

also how can I code creating a new checkbox and label for every x
in the table?
 
S

sweet_dreams

Dot Net Daddy napisal(a):
Hello,

I want to create label and checkbox for every member in the database
table.

How can I do that?

I tried:

Dim x as integer

For Each x In ds.Tables(0).Rows.Count
.... code here
Next


But it didnt work, giving an error:

Error 1 Expression is of type 'Integer', which is not a collection
type.

also how can I code creating a new checkbox and label for every x
in the table?

Hi,

First of all, you use For Each ... In ... to iterate through a
collection of items. In your code x's type should be DataRow and you
should interate through collection of rows like that:

dim x as DataRow

For Each x In ds.Tables(0).Rows
....
Next

or


Dim RowNumber as Integer=ds.Tables(0).Rows.Count

for i as integer=1 to RowNumber
....
Next


Secondly, I don't think creating many Labels and checkboxes is good
idea. If you want to show data from dataset it's beter to use GridView.
You can have there for example CheckBox field.

regards,
sweet_dreams
 
D

Dot Net Daddy

Thank you for your help.
I tried to do it with GridView but I couldn't, (I am a newbie)

For example, say I want to assign a label the value in the gridview
that has been selected. I tried


Protected Sub GridView1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
GridView1.SelectedIndexChanged
Label1.Text = GridView1.SelectedRow.Cells(0).ToString
End Sub

when I select a row Label1 is assigned the value:
"System.Web.UI.WebControls.DataControlFieldCell"

actually I want to assign the value in the selected row to an imageURL.
as far as I know GridView should be able to do it. but I couldnt get it
done.

thanks again for your help...
 
S

sweet_dreams

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
GridView1.SelectedIndexChanged
Label1.Text = GridView1.SelectedRow.Cells(0).ToString
End Sub

when I select a row Label1 is assigned the value:
"System.Web.UI.WebControls.DataControlFieldCell"

Actualy I don't use ASP but I think that it should be like that:
Label1.Text = GridView1.SelectedRow.Cells(0).Value

Hope this helps.

regards,
sweet_dreams
 

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

Similar Threads


Top