0

i'm trying to create a table that it's rows has textboxes to fill and save them in database. for it purpose i need to assign textboxes's id to an array and use return this array for click event of save button but this error occurred! is someone help me to fix it?

private string[][] GenerateTable(int colsCount, int rowsCount)
{
    string[][] controls_Array = new string[7][];            
    //Create the Table and Add it to the Page
    Table table = new Table();
    table.ID = "Table1";
    table.CellSpacing =5;
    table.BorderWidth = 0;

    Page.Form.Controls.Add(table);
    TableColoumnHeader(table,table.Rows);
    // Now iterate through the table and add your controls 
    for (int i = 0; i < rowsCount; i++)
    {
        TableRow row = new TableRow();
        Label lbl = new Label();
        lbl.Text = "row"+i.ToString();
        ///
        TableCell TC = new TableCell();
        TC.Controls.Add(lbl);
        row.Cells.Add(TC);
        for (int j = 0; j < colsCount; j++)
        {
            TableCell cell = new TableCell();

            // Add the control to the TableCell
            if(j==colsCount-1)
            {
                CheckBox chkb = new CheckBox();
                chkb.ID = "CheckBoxRow_" + i + "Cols_" + j;
                cell.Controls.Add(chkb);
                controls_Array[j][i] = chkb.ID;
            }
            else
            {    
                TextBox tb = new TextBox();
                // Set a unique ID for each TextBox added
                tb.ID = "TextBoxRow_" + i + "Col_" + j;
                cell.Controls.Add(tb);
                controls_Array[j][i] = tb.ID;// this line where is the mentioned error occurred.
            }

            // Add the TableCell to the TableRow
            row.Cells.Add(cell);
        }
        // Add the TableRow to the Table
        table.Rows.Add(row);

    }/**/
    return controls_Array;

}
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Javad
  • 41
  • 1
  • 2
  • 6
  • Whenever an exception occured you will also get a `StackTrace`, which will tell you exactly what file and line number the exception occured. Attach the debugger to your application and put a breakpoint on that line, and see why it's throwing a `NullReferenceException`. - This assumes you aren't compiling as *Release*. – aevitas Feb 08 '14 at 18:27
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Alberto Feb 08 '14 at 19:15

1 Answers1

2

Your controls_Array is initialized only for the first dimension to a fixed value (7).
So it is like you have declared 7 arrays but each of them has no dimension.

When you try to insert a value in the first element of the second dimension, you get the null reference because the array has no size for this dimension.

It seems that you want to fill it with data from Rows and Columns.
So the easiest way to fix your code is to initialize the array correctly with rows and columns

Start giving the size for the rows

string[][] controls_Array = new string[rowsCount][];

and before entering the inner loop dimension the array for the columns of the current row [i]

controls_Array[i] = new string[colsCount];
for (int j = 0; j < colsCount; j++)
{
   .....
}

Also I think you need to switch the indexers when you insert your items inside the array, because the first dimension of the array represents the rows (indexer i) while the second dimension the columns (indexer j)

controls_Array[i][j] = tb.ID;

and

controls_Array[i][j] = chkb.ID;
Steve
  • 213,761
  • 22
  • 232
  • 286