sujeesh
sujeesh

Reputation: 498

Adding row in asp.net table

I have the following code in my .aspx file

<asp:Table ID="tabStudent" runat="server"></asp:Table>

I declared all variables which I used below. I wrote the following code snippet in the page load event. But it only shows only one row which having last row in my database. But when I am using message box inside this while loop to print those values I am getting all the rows.

Reader = Command.ExecuteReader()
While Reader.Read()
    lblRollNo.Text = Reader.Item(0)
    lblName.Text = Reader.Item(1)
    lblDob.Text = Reader.Item(2)
    tcRollNo.Controls.Add(lblRollNo)
    tcName.Controls.Add(lblName)
    tcDob.Controls.Add(lblDob)
    TableRow.Cells.Add(tcRollNo)
    TableRow.Cells.Add(tcName)
    TableRow.Cells.Add(tcDob)
    tabStudent.Rows.Add(TableRow)
End While

what is wrong with this code?

Upvotes: 1

Views: 2608

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460340

You are not creating new controls in the loop but you're always reusing the same.

While Reader.Read()
    TableRow = New TableRow()
    lblRollNo = New Label()
    tcRollNo = New TableCell()
    ' and so on ...
    lblRollNo.Text = Reader.Item(0)
    lblName.Text = Reader.Item(1)
    lblDob.Text = Reader.Item(2)
    tcRollNo.Controls.Add(lblRollNo)
    tcName.Controls.Add(lblName)
    tcDob.Controls.Add(lblDob)
    TableRow.Cells.Add(tcRollNo)
    TableRow.Cells.Add(tcName)
    TableRow.Cells.Add(tcDob)
    tabStudent.Rows.Add(TableRow)
End While

Upvotes: 1

Related Questions