how can add data using for loop

jewel 801 Reputation points
2024-04-18T10:36:25.6733333+00:00
I want to insert some array data into the database. Here using for loop. The problem is - I want to keep the InvoiceNO related to the OrderNumber change. For example, if the OrderNumber changes, the InvoiceNO  will also change. I have shown it in the picture.
I would appreciate it if someone could help.

 public IActionResult addtoSelltable(int[] _ORderid, int[] _productid, string[] _OrderNumber)
 {
     for (int i = 0; i < _ORderid.Count(); i++)
     {
         int inv = 10001;
         var pro = new tbl_Sell();
         pro.Date = DateTime.Now;
         pro.productID = _productid[i];
         pro.OrderNumber = _OrderNumber[i];
         pro.InvoiceNO = Convert.ToString( inv+1);//my problem is here
         _context.tbl_Sells.Add(pro);
         _context.SaveChanges();
     }
     return View("SeleDetails");
 }

Untitled.png
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,177 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,526 Reputation points
    2024-04-18T15:48:06.9233333+00:00

    as stated just move the initial invoice value out of the loop. you also need to use array indexing. a rename of orderNum to lastOrderNum would help reader of code:

         string lastOrderNum = ""; 
         int inv = 10001;
         for (int i = 0; i < _ORderid.Count(); i++)
         {
             var pro = new tbl_Sell();
             pro.Date = DateTime.Now;
             pro.productID = _productid[i];
             pro.OrderNumber = _OrderNumber[i];
             if(lastOrderNum == _OrderNumber[i])
             {
                pro.InvoiceNO = Convert.ToString(inv);
             }
             else
             {
                pro.InvoiceNO = Convert.ToString(++inv);
                lastOrderNum = _OrderNumber;           
             }
             _context.tbl_Sells.Add(pro);
         }
         _context.SaveChanges();
    

    note: this code will only work if the _product, _OrderNumber arrays are at least as large as _ORderid array. also it will fail, if the _OrderNumber array is not in order. if the _OrderNumber is not sorted, then use a dictionary.

         var orderInvoiceNumber = new Dictionary<string,int>(); 
         int inv = 10001;
         for (int i = 0; i < _ORderid.Count(); i++)
         {
             var pro = new tbl_Sell();
             pro.Date = DateTime.Now;
             pro.productID = _productid[i];
             pro.OrderNumber = _OrderNumber[i];
             if(orderInvoiceNumber.ContainsKey(_OrderNumber[i]))
             {
                pro.InvoiceNO = orderInvoiceNumber[_OrderNumber[i]];
             }
             else
             {
                pro.InvoiceNO = Convert.ToString(++inv);
                orderInvoiceNumber[_OrderNumber[i]] = inv;           
             }
             _context.tbl_Sells.Add(pro);
         }
         _context.SaveChanges();
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Brando Zhang-MSFT 2,956 Reputation points Microsoft Vendor
    2024-04-18T12:08:41.37+00:00

    Hi @jewel,

    You could try below code:

                int inv = 10001;
                for (int i = 0; i < _ORderid.Count(); i++)
                {
                
                    var pro = new tbl_Sell();
                    pro.Date = DateTime.Now;
                    pro.productID = _productid[i];
                    pro.OrderNumber = _OrderNumber[i];
                    inv = inv + 1;
                    pro.InvoiceNO = Convert.ToString(inv);//my problem is here
    
                    _context.tbl_Sells.Add(pro);
                    _context.SaveChanges();
                }
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments