Solved

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

Posted on 2010-08-26
4
1,063 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
Comment Utility
verify that 'ie' has elements in it before you submit with the debugger
0
 

Author Comment

by:frtools
Comment Utility
ie does recieve values.

ie-watch.jpg
0
 

Author Comment

by:frtools
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

12 Experts available now in Live!

Get 1:1 Help Now