frustratedInFresno
frustratedInFresno

Reputation: 47

c# do if statments break loops

Datasources: Datatable with employee information

I'm trying to group my data by employee name, then output the employee name and below that output the records associated with that employee and only that employee.

I've got an if statement within a nested for each which is within another for each (I also tried regular for loops) . The if statement checks the employee name against the current name it's iterating with and outputs the record's information if it is true. This works great for the first record of each employee. After the first iteration per employee it breaks and process of elimination has shown me that it's because of the if statement. It's like using an if statement breaks the loop.


public String GroupAndSortTableData(DataTable thisDataTable)
    {
        //detailActivitiesDataTable datatable column key
        //0 = currentID, 1 = formerID, 2 = Description, 3 = MonthID, 4 = Year, 5 = CreatedBy, 6 = CreatedOn, 7 = ModifiedBy, 
        //8 = ModifiedOn, 9 = UserID, 10 = Signed, 11 = Type, 12 = Software, 13 = Activity, 14 = DeleteFlag, 15 = DeletedBy, 16 = DeletedOn
        //17 = UserName, 18 = SortName, 19 = FullName
        try
        {
            System.Text.StringBuilder strReport = new System.Text.StringBuilder();
            //get unique values in the SortName column of detailActivitiesDataTable
                         group row by row.Field<string>("SortName") into grp
                         select new
                         {
                             SortName = grp.Key,
                             //MemberCount = grp.Count()
                         };
            strReport.AppendLine("<table width='800px' class='light' cellspacing='0' cellpadding='0' border='0'><tr><th align='left' colspan='4'> A C T I V I T I E S</th></tr></table>");
            strReport.AppendLine("<table width='800px' class='light' cellspacing='0' cellpadding='0' border='0'><tr><th align='left' colspan='4'><hr></th></tr></table>");
            //display a subheader bar for each name in the data
            foreach (var uniqueName in result)
            {
                //testing messagebow works in localhost only    
                strReport.AppendLine("<table class='dark' width='800px'><tr><th colspan='4' align='left'>" + uniqueName.SortName + "</th></tr>");
                strStaffActivities.AppendLine("<tr><td colspan='4' align='left'><hr width='800px'></td></tr>");
                strReport.AppendLine("<tr>"
                                                + "<td class='lightHeader2' width='150'>Type</td>"
                                                + "<td class='lightHeader2' width='150'>Activity</td>"
                                                + "<td class='lightHeader2' width='150'>Software</td>"
                                                + "<td class='lightHeader2' width='350'>Description</td>"
                                                + "</tr></table>");
                strReport.AppendLine("<table class='light' width='800px'>");
                //loop through the rows of the datatable 
                foreach(DataRow dtRow in thisDataTable.Rows)
                {
                    //if it matches the username display the content
                    if (dtRow[18] == uniqueName.SortName)
                    {
                        strReport.AppendLine("<tr>");
                        strReport.AppendLine("<td class='light' width='150'>" + dtRow[11] + "</td>");
                        strReport.AppendLine("<td class='light' width='150'>" + dtRow[13] + "</td>");
                        strReport.AppendLine("<td class='light' width='150'>" + dtRow[12] + "</td>");
                        strReport.AppendLine("<td class='light' width='350'>" + dtRow[2] + "</td>");
                        strReport.AppendLine("<td class='light' width='350'>" + dtRow[18] + "</td>");
                        strReport.AppendLine("<td class='light' width='350'>" + uniqueName.SortName + "</td>");
                        strReport.AppendLine("</tr>");
                    }
                    else
                        continue;
                }

            }
            strReport.AppendLine("</table>");
            return strReport.ToString();
        }
        catch (Exception eGroupActivitiesDetail)
        {
            var resultException = MessageBox.Show("Exception occurred while grouping and sorting data." + eGroupActivitiesDetail);
            return "failure";
        }
    }

Upvotes: 0

Views: 113

Answers (2)

frustratedInFresno
frustratedInFresno

Reputation: 47

Found the answer, there was bad data in the datatable.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

An if statement can "break the loop" if its condition contains an instruction that triggers an exception.

Your if (dtRow[18] == uniqueName.SortName) is a good suspect for producing an "index out of bounds" exception in case dtRow does not have the element at index 18. You should add a check to see if the index is valid before referencing it to avoid throwing an exception.

Upvotes: 2

Related Questions