1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.