Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-08-26
4
Medium Priority
?
1,090 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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