Solved

c # Insert all on Submit does not insert List - Linq

Posted on 2010-08-26
4
1,071 Views
Last Modified: 2013-11-11
I'm reading in an excel file and there is data in it and it does read the data. When I try to create a list with the excel data the insert on submit does nothing.

ShippingDataContext db = new ShippingDataContext();
            PackedOrder po = new PackedOrder();
            List<PackedOrder> ie = new List<PackedOrder>();

            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
            {
               
                try
                {
                    while (((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
                    {
                        rowIndex = 1 + index;
                        string ordernum = ((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
                        po.OrderNum = ordernum;
                        po.ShipperID = packID;
                        ie.Add(po);
                        index++;
                    }
                    db.PackedOrders.InsertAllOnSubmit(ie);
                    db.SubmitChanges();
                }
                catch
                {
                    app.Quit();
                }

                ts.Complete();

            }
0
Comment
Question by:frtools
  • 2
4 Comments
 
LVL 21

Expert Comment

by:naspinski
ID: 33536619
verify that 'ie' has elements in it before you submit with the debugger
0
 

Author Comment

by:frtools
ID: 33536976
ie does recieve values.

ie-watch.jpg
0
 

Author Comment

by:frtools
ID: 33537016
I moved the submit all outside of the try{} catch{}, but it only inserts the last row.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33537820
Hi frtools;

You need to move this line in your code:

PackedOrder po = new PackedOrder();

To the begining of the While loop as shown in the code snipett  below. You need to create a new PackedOrder for each item you want to insert to the database and NOT use the same object over and over again.

Fernando
try
{
    while (((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
    {
        PackedOrder po = new PackedOrder();
        rowIndex = 1 + index;
        string ordernum = ((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
        po.OrderNum = ordernum;
        po.ShipperID = packID;
        ie.Add(po);
        index++;
    }
    db.PackedOrders.InsertAllOnSubmit(ie);
    db.SubmitChanges();
}

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now