?
Solved

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

Posted on 2010-08-26
4
Medium Priority
?
1,086 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 63

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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