Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends i am getting outofmemoryexception, Here is my code which is in italic i have to optimized these nested loops to avoid the exception.

I am telling the background here ,I have a condition in which i have Dataset A,Dataset B and Dataset C.Dataset A is parent & the remaing are childs,I have to merge Dataset B & dataset C in Dataset A wrt loanmasterid.


case Constants.Reports.UHrptLoanSummarybatch:
                  rptData = dbHelper.Get_Loan_Summary_Batch(objReportHelper.CustomerId, objReportHelper.AuditId);//  parent  table
                  rptData2 = dbHelper.Get_Other_Comments_Batch(objReportHelper.CustomerId, objReportHelper.AuditId);
                  rptData4 = dbHelper.Get_Audit_Details_Batch(objReportHelper.CustomerId, objReportHelper.AuditId);

                  rptData.DataSetName = "spReport_LoanSummaryBatch_edit";

                  foreach (DataColumn col in rptData2.Tables[0].Columns)
                  {
                      if(!rptData.Tables[0].Columns.Contains(col.ColumnName))
                          rptData.Tables[0].Columns.Add(col.ColumnName);
                  }

                  foreach (DataColumn col in rptData4.Tables[0].Columns)
                  {
                      if(!rptData.Tables[0].Columns.Contains(col.ColumnName))
                          rptData.Tables[0].Columns.Add(col.ColumnName);
                  }

                  DataRow [] dRows;
                  DataRow parentRow;
                  DataRow newRow;

                  //for (int i = 0; i < rptData.Tables[0].Rows.Count; i++)
                  //{
                  //    parentRow = rptData.Tables[0].Rows[i];

                  //    dRows = rptData2.Tables[0].Select("loanmasterid=" + parentRow["loanmasterid"].ToString());

                  //    foreach (DataRow childRow in dRows)
                  //    {
                  //        newRow = rptData.Tables[0].NewRow();

                  //        foreach (DataColumn childCOl in rptData2.Tables[0].Columns)
                  //        {
                  //            newRow[childCOl.ColumnName] = childRow[childCOl.ColumnName];
                  //        }

                  //        rptData.Tables[0].Rows.Add(newRow);

                  //    }
                  //}

                  for (int i = 0; i < rptData.Tables[0].Rows.Count; i++)
                  {
                      parentRow = rptData.Tables[0].Rows[i];

                      dRows = rptData4.Tables[0].Select("loanmasterid=" + parentRow["loanmasterid"].ToString());

                      foreach (DataRow childRow in dRows)
                      {
                         newRow = rptData.Tables[0].NewRow();

                          foreach (DataColumn childCOl in rptData4.Tables[0].Columns)
                          {
                              newRow[childCOl.ColumnName] = childRow[childCOl.ColumnName];
                          }

                          rptData.Tables[0].Rows.Add(newRow);

                      }
                  }






                  objReportDataSource = new ReportDataSource("spReport_LoanSummaryBatch_edit", rptData4.Tables[0]);



                  objReportDataSource4 = new ReportDataSource("spReport_LoanSummaryBatch_edit", rptData4.Tables[0]);





                  ReportViewer1.LocalReport.ReportPath = "Reports/UHrptLoanSummarybatch.rdlc";

                  break;
Posted
Updated 3-Nov-12 11:13am
v2

1 solution

Um. I'm not surprised.

Your code is effectively
C#
int j = 2;
for (int i = 0; i < j; i++)
   {
   j++;
   }
because inside your for loop you add a variable number of rows to rptData.Tables[0] and then use the number of rows in the table to control the loop. So if you ever get to the position where a row causes a copy of itself to be added to the table, then it will start an infinite loop, and you will run out of memory because you create new objects each time round.

I don't know what you are trying to do, but I can't help thinking that you are going about it the wrong way!
 
Share this answer
 
Comments
Ali_100 4-Nov-12 13:32pm    
OMG ,You are a great man,I was screwing it how to optimize it,ohhh Thank you so much
OriginalGriff 5-Nov-12 2:25am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900