Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
using (System.Data.Entity.DbContextTransaction dbTran = db.Database.BeginTransaction())
               {
                   try
                   {
                       foreach (var items in obj.ListOfItems)
                       {
                           _dbListModel.ItemMainCategory = items.ItemMainCategory;
                           _dbListModel.ItemSubCateogry = items.ItemSubCateogry;
                           _dbListModel.Quantity = items.Quantity;
                           _dbListModel.ItemName = items.ItemName;
                           _dbListModel.ItemPurpose = items.ItemPurpose;
                           _dbListModel.IsIssue = false;
                           _dbListModel.RequestNo = obj.MaterialRequest.RequestNo;
                           // check weather available in store or not
                           _dbListModel.IsAvailable = CheckStock(items.ItemName, items.Quantity);

                           db.tbl_RequestedMaterialDetial.Add(_dbListModel);
                           //db.SaveChanges();
                       }
                       _dbModelRequestDetail.RequestNo = obj.MaterialRequest.RequestNo;
                       _dbModelRequestDetail.WorkOderNo = obj.MaterialRequest.WorkOderNo;
                       _dbModelRequestDetail.TicketId = obj.MaterialRequest.TicketId;
                       _dbModelRequestDetail.ChassisNo = obj.MaterialRequest.ChassisNo;
                       _dbModelRequestDetail.RequstedDate = obj.MaterialRequest.RequstedDate;
                       _dbModelRequestDetail.Purpose = obj.MaterialRequest.Purpose;
                       _dbModelRequestDetail.Model = obj.MaterialRequest.Model;
                       _dbModelRequestDetail.Type = obj.MaterialRequest.Type;
                       _dbModelRequestDetail.ComputerName = GetUserIP();
                       _dbModelRequestDetail.Send = "F";
                       _dbModelRequestDetail.Status = "New Request";

                       _dbModelRequestDetail.Dept = obj.MaterialRequest.Dept;
                       _dbModelRequestDetail.Designation = obj.MaterialRequest.Designation;
                       _dbModelRequestDetail.EmpID = obj.MaterialRequest.EmpID;
                       _dbModelRequestDetail.RequestPersonName = obj.MaterialRequest.RequestPersonName;
                       db.tbl_MaterialRequestForm.Add(_dbModelRequestDetail);
                       // change the status of the items in requsiton and store

                       //The Transaction will be completed
                       db.SaveChanges();

                       //commit transaction
                       dbTran.Commit();
                   }

                   catch (Exception ex)
                   {
                       //Rollback transaction if exception occurs
                       dbTran.Rollback();
                   }


What I have tried:

I want to save all Incoming records from list but it saved latest record in the list can any one help me please
Posted
Updated 5-Nov-18 1:55am
Comments
CHill60 5-Nov-18 7:56am    
Because your save of the changes is not within the loop that iterates through all the items!

1 solution

Simple: you only have one item, and you keep reusing it.
Imagine you have an envelope and into that you place cards, each of which contains a letter of the alphabet.
You place the letter "A" into the envelope and pass it to your mate who notes down the number on the outside of the envelope.
Then you grab it back before he can look at it, and stuff the "B" in there. But ... the envelope can only hold one letter, so you have to remove the "A" first and throw it in the bin. Then you hand the envelope to your mate, who notes down the number on the outside of the envelope.
Then you grab it back before he can look at it, and stuff the "C" in there. But ... the envelope can only hold one letter, so you have to remove the "B" first and throw it in the bin. Then you hand the envelope to your mate, who notes down the number on the outside of the envelope.
This repeats until you have used the "Z".

Your mate looks at his list of envelope numbers, and opens the envelope that matches any one of them - it holds the last letter, and none of the others.

That's what you have done here - you add the same object to your collection each time round the loop, but then you throw away the content in favour of the new.
If you want to add different data each time, you need to create a new instance of your class in _dbListModel each time you go round the loop.
C#
foreach (var items in obj.ListOfItems)
   {
   _dbListModel = new ... ();
   _dbListModel.ItemMainCategory = items.ItemMainCategory;
...
 
Share this answer
 
Comments
Aitzaz Ahsan 5-Nov-18 12:26pm    
can you explore more please how i can save all items at same time in the database please

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