Henry Nyoike

Henry Nyoike

  • NA
  • 96
  • 573

Generating bulk barcode and inserting to db

Mar 18 2018 11:52 PM
I have a form that receives and inserts into a database input of a product in this case a batch of booklet from a supplier. The product fields are batch number, supplier name, Serial Numbers, Quantity of booklets Received(PCs inside a batch).
 
I am able to insert the input into table booklets but I also want to generate Barcodes based on the number of quantity of booklets received and store it in a second table called Barcode table that captures the details of a single booklet. e.g If I received 10 booklets, I want to generate 10 Barcodes and store in a second table. I am able to generate Barcode for a single booklet but in this case I want to generate multiple number of Barcodes based on the number of Quantity of booklets Received. How do I loop through the quantity received number generate these multiple Barcodes and store in table Barcode ?
 
My create function inside a Controller to insert data into both tables at the same time.
  1. [HttpPost]  
  2.  [ValidateAntiForgeryToken]   
  3. public ActionResult Create(bts_receivebookletfromsupplier bts_receivebookletfromsupplier)  
  4.  {   
  5. //Barcodes   
  6.  Barcodecs objbar = new Barcodecs();   
  7.       Barcode barcode = new Barcode();   
  8.       barcode.Barcode1 = objbar.generateBarcode();   
  9.       barcode.Name = bts_receivebookletfromsupplier.BatchNo + "-" + DateTime.Now.ToString();   
  10.       barcode.Description = DateTime.Now.ToString();  
  11.       barcode.BarcodeImage = objbar.getBarcodeImage(objbar.generateBarcode(), barcode.Name);  
  12.      if (ModelState.IsValid) {   
  13.          db.bts_receivebookletfromsupplier.Add(bts_receivebookletfromsupplier);  
  14.           //Generate barcodes based on the number of quantity received    
  15.            db.Barcodes.Add(barcode);   
  16.            db.SaveChanges();   
  17.          TempData["Success"] = bts_receivebookletfromsupplier.QtyReceived + " Booklets  successfully recieved";  
  18.          return RedirectToAction("Index");   
  19.          } else {   
  20.            ModelState.AddModelError("""Error submiting. Submission Aborted");   
  21.         }  
  22.         return View(bts_receivebookletfromsupplier);   
  23.       }   
  24. }