Solved

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

Posted on 2010-08-26
4
1,075 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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