Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:39:01+00:00 2026-05-16T00:39:01+00:00

I am building an ASP.NET application that needs dynamic tables. That’s another issue that

  • 0

I am building an ASP.NET application that needs dynamic tables. That’s another issue that I’ve already posted about (and gotten a pretty good response!). Now, I’m running into another issue – I want to add new rows to my table, but given that I will have 10-12 tables on one page, each containing different objects in their rows (text boxes, check boxes, etc.) I need a way of simply generically adding a new row that has the same objects as the first row in the table. Here’s my code:

Private Sub AddTableRow(ByRef originalTable As System.Web.UI.WebControls.Table)

        Dim originalRow As System.Web.UI.WebControls.TableRow = originalTable.Rows(1)
        Dim insertingRow As New System.Web.UI.WebControls.TableRow
        Dim insertingCells(originalRow.Cells.Count) As System.Web.UI.WebControls.TableCell
        Dim index As Integer = 0

        For Each cell As System.Web.UI.WebControls.TableCell In originalRow.Cells

            insertingCells(index) = New System.Web.UI.WebControls.TableCell
            insertingCells(index).Controls.Add(cell.Controls.Item(0))

            index += 1
        Next

        insertingRow.Cells.AddRange(insertingCells)

        originalTable.Rows.Add(insertingRow)

    End Sub

But I’m getting a null reference exception at the second to last line,

insertingRow.Cells.AddRange(insertingCells)

…and I can’t figure out why. Is it because the contents of each cell are not being initialized with a new object? If so, how would I get around this?

Thanks!

EDIT:

The inside of my for loop now looks like this –

For Each cell As System.Web.UI.WebControls.TableCell In originalRow.Cells
        Dim addedContent As New Object
        Dim underlyingType As Type = cell.Controls.Item(0).GetType

        addedContent = Convert.ChangeType(cell.Controls.Item(0), underlyingType)
        insertingCells(index) = New System.Web.UI.WebControls.TableCell
        insertingCells(index).Controls.Add(addedContent)

        index += 1
Next

Stepping through with a debugger, I see that this strategy is working – but the additional table row still doesn’t appear…and still does when I do this statically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:39:02+00:00Added an answer on May 16, 2026 at 12:39 am

    I think your culprit may be this line:

    Dim insertingCells(originalRow.Cells.Count) As TableCell
    

    Confusingly, the number you specify in an array declaration in VB.NET is the upper bound, not the number of elements. So Dim ints(10) As Integer will create an Integer() array with eleven elements, not ten (10 will be the highest index of the array).

    Try this instead:

    Dim insertingCells(originalRow.Cells.Count - 1) As TableCell
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.