Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Exception memory
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 3-Nov-12 12:09pm
Ali_1001.3K
Edited 3-Nov-12 12:13pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Um. I'm not surprised.
 
Your code is effectively
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!
  Permalink  
Comments
Ali_100 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 220
3 Praneet Nadkar 197
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100