How to avoid multiple duplicate interation from data table in c#

Gani_tpt 2,006 Reputation points
2020-12-05T02:59:43.29+00:00
int rowidx = 0;  
//process for each Main Product list  
mainproduct = //get data table mainproduct of "prod no"  
//so mainproduct list is "102201"  
  "102202"  
                         "102203"  
                         "102204"  
foreach (int prod in mainproduct)  
{  
  
        //Get sub product no from main data table (DTmainProduct). for example  
 string prodno = "102201";  
        string proname = "ALKO";  
        string ItemNo1 = "A-1001222";  
  
 foreach (DataRow DR in DTsubProduct.Rows)  
 {  
  //Get sub product no from main data table (DTsubProduct). for example  
  if ((ItemNo1==ItemNo2)  
  {  
  
                 }  
  else  
                 {  
                    //if not matching the ItemNo, handling the error log  
                    DataRow dr = ErrReport.NewRow();  
                    ErrReport.Rows.Add(dr);  
                    bool singleRowEnough = false;  
             List<string> ErrorList = new List<string>();  
                    foreach (DataRow row in ErrReport.Rows)  
                    {  
                         if (singleRow) break;  
                         ErrorList.Clear(); // clear before add values  
                         ErrReport.Rows[rowidx][0] = prodno;  
                         ErrReport.Rows[rowidx][1] = proname;  
                         string msg1 = "Item Not Found in Sub Product Table";  
                         ErrorList.Add("Error1" + "=" + msg1);  
  
                         singleRowEnough = true;  
                    }  
     rowidx++;  
                 }  
 }  
  
}  

if ItemNo1 value is "A-1001222" from main table, have to check in aonther sub product table.

if value "A-1001222" does not exist in "DTsubProduct" table, then we have to handle the error

like "Item Not Found in Sub Product Table"

My requirement is , Error Report part should always come and check the error if any found based on condition once.

While checking and executing the error part, it will execute only once and close the "DTsubProduct" loop.
other wise, the same entry will repeat multiple times.

How to do this..?

sample data for reference below.

45393-dtmainproduct.jpg

45394-dtsubproduct.jpg

45247-finalerrorreport.jpg

The final Error output would be like above attachment.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,458 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bonnie DeWitt 811 Reputation points
    2020-12-06T00:05:58.677+00:00

    Hi @GaniTPT ,

    I'm not sure that I understand your question, but I did notice a few things that don't look right. I don't think you need the loop through the ErrReport table. Maybe you want your loops to look more like this:

    // The ErrorList needs to be created outside both loops  
    List<string> ErrorList = new List<string>();  
    foreach (int prod in mainproduct)  
    {  
          
        //Get sub product no from main data table (DTmainProduct). for example  
        string prodno = "102201";  
        string proname = "ALKO";  
        string ItemNo1 = "A-1001222";  
      
        foreach (DataRow DR in DTsubProduct.Rows)  
        {  
            //Get sub product no from main data table (DTsubProduct). for example  
            if ((ItemNo1==ItemNo2)  
            {  
                // You might be doing something if they match, I don't know  
            }  
            else  
            {  
                //if not matching the ItemNo, handling the error log  
                DataRow dr = ErrReport.NewRow();  
                ErrReport.Rows.Add(dr);  
                dr[0] = prodno;  
                dr[1] = proname;  
            }  
        }      
    }  
    

    Hope that helps.

    0 comments No comments