2

I have a DataTable which I fill from a database and the in code behind I am trying to add 3 more rows after each row. Below is the code. But at the 6th line I get

Exception of type 'System.OutOfMemoryException' was thrown.

  for (int i = 0; i < AlldaysList.Rows.Count; i++)
    {
        DataRow row;
        row = AlldaysList.NewRow();
        DataRow row1;
        row1 = AlldaysList.NewRow();
        DataRow row2;
        row2 = AlldaysList.NewRow();

        // Then add the new row to the collection.
        row["scenarionid"] = DBNull.Value;
        row["description"] = "";
        row1["scenarionid"] = DBNull.Value;
        row1["description"] = "";
        row2["scenarionid"] = DBNull.Value;
        row2["description"] = "";
        AlldaysList.Rows.InsertAt(row, i + 1);
        AlldaysList.Rows.InsertAt(row1, i + 2);
        AlldaysList.Rows.InsertAt(row2, i + 3);
        i++;
    }
2
  • 4
    why u are doing i++ at the end of for loop ?? Commented Jan 28, 2013 at 9:56
  • for (int i = 0; i < AlldaysList.Rows.Count; i++).... modify this to for (int i = 0; i < AlldaysList.Rows.Count + 2; i++).. Commented Jan 28, 2013 at 9:57

3 Answers 3

4
//This could be the problem
i < AlldaysList.Rows.Count

i think u should have a variable called int rowCount = AlldaysList.Rows.Count; before the loop..

the loop should be  for (int i = 0; i < rowCount; i++)

The reason why i say this is because if u add 3 rows inside the loop ur AlldaysList.Rows.Count is changing by +3 and u r targeting a dynamic variable instead of a static one and so it goes into the loop again and causes an exception..

Sign up to request clarification or add additional context in comments.

Comments

1

I think you should do something like this:

int origRowCount = AlldaysList.Rows.Count;
for (int i = 0; i < origRowCount; i++)
{
    for (int j = 1; j <= 3; j++)
    {
        AlldaysList.Rows.InsertAt(MakeNewAlldaysRow(AlldaysList), i * 4 + j);
    }
}

// ....
// (separate method)
static DataRow MakeNewAlldaysRow(DataTable table)
{
    DataRow row = table.NewRow();
    row["scenarionid"] = DBNull.Value;
    row["description"] = "";

    return row;
}

Since the list of rows is going to be increasing, you need to make a note of the row count before you start adding rows. Also, the insert locations are going to be increasing by 4, hence i * 4 + j.

Comments

0

Generalised version of your code, you can add any number of rows by just changing the value of variable RowsToAdd. You dn't need crate three DataRow variable(row,row1,row2)...

int RowsToAdd=3
int rowCount = AlldaysList.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
   for (int j = 0; j < RowsToAdd; j++)
   {
     DataRow dr = AlldaysList.NewRow();
     dr["scenarionid"] = DBNull.Value;
     dr["description"] = "";

     AlldaysList.Rows.Add(dr);
   }
}

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.