Click here to Skip to main content
15,887,476 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I am having a list. In my list I have only 1 record.I want to add an increment counter in a list. If user enter 5 value the same recor will be display 5 times.

1 INDIA
2 INDIA
3 INDIA
4 INDIA
5 INDIA


Example: IF value of NdPlettes= 4. my list will display 4 times.
IF value of NdPlettes= 5. my list will display 5 times

now I am getting result like:
5 INDIA
5 INDIA
5 INDIA
5 INDIA
5 INDIA

What I have tried:

int index = 1;
               generateSeqNumber();
               foreach (var ProdSelection in ClientImpressionProdSelection)
               {
                   if (index <= InputProduct.NdPlettes)
                   {
                       ProdSelection.Sequence = Convert.ToInt32(GenResult);
                       ProdSelection.Number = index;
                       ProdSelection.ClientDestinataire = InputProduct.ClientDestinataire;
                       ProdSelection.LieuDeLivraison = InputProduct.LieuDeLivraison;
                       ProdSelection.CodeProduitClient = InputProduct.CodeProduitClient;
                       ProdSelection.CodeCouleurClient = InputProduct.CodeCouleurClient;
                       ProdSelection.CodeFournisseurEMPourClient = InputProduct.CodeFournisseurEMPourClient;
                       ProdSelection.AQP = InputProduct.AQP;
                       ProdSelection.Produit = InputProduct.Produit;
                       ProdSelection.RefFournisseur = InputProduct.RefFournisseur;
                       ProdSelection.NdShipment = InputProduct.NdShipment;
                       ProdSelection.NdLot = InputProduct.NdLot;
                       ProdSelection.Cdate = InputProduct.Cdate;
                       ProdSelection.PoidsNet = InputProduct.PoidsNet;
                       ProdSelection.PoidsBrut = InputProduct.PoidsBrut;
                       ProdSelection.NbrPallet = InputProduct.NdPlettes;
                       ProdSelection.Material = InputProduct.Material;
                       ProdSelection.CodClient = InputProduct.CodClient;
                       ProdSelection.CodPackaging = InputProduct.CodPackaging;
                       ProdSelection.CoefNetBrut = InputProduct.CoefNetBrut;
                       index++;
                       GetClientImpressionProductSel.Add(ProdSelection);
                   }
Posted
Updated 20-Nov-18 1:08am
v2

Your loop processes all records - but it only processes records that are there. If you want to process records multiple times, then you will need an external loop as well:
C#
while (index <= InputProduct.NdPlettes)
{
   foreach (var ProdSelection in ClientImpressionProdSelection)
   {
       if (index++ > InputProduct.NdPlettes) break;
       ProdSelection.Sequence = Convert.ToInt32(GenResult);
       ProdSelection.Number = index;
       ProdSelection.ClientDestinataire = InputProduct.ClientDestinataire;
       ProdSelection.LieuDeLivraison = InputProduct.LieuDeLivraison;
       ProdSelection.CodeProduitClient = InputProduct.CodeProduitClient;
       ProdSelection.CodeCouleurClient = InputProduct.CodeCouleurClient;
       ProdSelection.CodeFournisseurEMPourClient = InputProduct.CodeFournisseurEMPourClient;
       ProdSelection.AQP = InputProduct.AQP;
       ProdSelection.Produit = InputProduct.Produit;
       ProdSelection.RefFournisseur = InputProduct.RefFournisseur;
       ProdSelection.NdShipment = InputProduct.NdShipment;
       ProdSelection.NdLot = InputProduct.NdLot;
       ProdSelection.Cdate = InputProduct.Cdate;
       ProdSelection.PoidsNet = InputProduct.PoidsNet;
       ProdSelection.PoidsBrut = InputProduct.PoidsBrut;
       ProdSelection.NbrPallet = InputProduct.NdPlettes;
       ProdSelection.Material = InputProduct.Material;
       ProdSelection.CodClient = InputProduct.CodClient;
       ProdSelection.CodPackaging = InputProduct.CodPackaging;
       ProdSelection.CoefNetBrut = InputProduct.CoefNetBrut;
       GetClientImpressionProductSel.Add(ProdSelection);
   }
}
 
Share this answer
 
Comments
Member 9956700 16-Nov-18 4:26am    
Its Not working correctly. Its showing records 4 times.
4 INDIA
4 INDIA
4 INDIA
4 INDIA

But I want

1 INDIA
2 INDIA
3 INDIA
4 INDIA
OriginalGriff 16-Nov-18 4:32am    
Then I suspect your code doesn't look like mine ... use the debugger, and look at exactly what is going on while it is running.
Member 9956700 16-Nov-18 4:35am    
While adding in a list i am getting 4 rows. But "ProdSelection.Number = index;". This is not displaying like 1, 2,3,4 instead its showing 4,4,4,4.
OriginalGriff 16-Nov-18 4:57am    
Of course it isn't!
You are reusing the same object each time you add it to your collection!
If you want different values, then you need to create a new instance each time you start filling it, and add that to your collection.

You currently change the foreach loop instance, and since that's always the same one, it will always retain the last value to set it to.
Put creation of the ProdSelection inside of the loop that you have, and created one for each iteration.

Good luck
 
Share this answer
 

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