?
Solved

Entity loop error

Posted on 2011-02-11
7
Medium Priority
?
528 Views
Last Modified: 2012-05-11
When I try to execute the follwoing code I get the follwoing error.

An object with the same key already exists in the ObjectStateManager. The existing object is in the Modified state. An object can only be added to the ObjectStateManager again if it is in the added state. How can I solve the problem?

Pls help.

               for (int i = 0; i < uploadedFiles.Count; i++)
                {
                    HttpPostedFile userPostedFile = uploadedFiles[i];

                    if (userPostedFile.ContentLength > 0)
                    {
                        myWorkDocs.Filename = guidResult;
                        myWorkDocs.UploadedBy = User.Identity.Name;
                        myWorkDocs.UploadedOn = DateTime.Now;

                        myEntity2.AddToWorkOrderDocs(myWorkDocs); <-----error here
                        myEntity2.SaveChanges();
                       
                    }
                       
                }

Open in new window

0
Comment
Question by:ayha1999
[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
  • 3
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34873575
It is difficult to say without seeing more of the code.

Where is this object created, myWorkDocs?
This looks like a function you created AddToWorkOrderDocs, can you post the code for it?

From the error it seems that you are trying to insert a record that already exist in the ObjectContext with the same primary key.
0
 
LVL 9

Accepted Solution

by:
gdupadhyay earned 1200 total points
ID: 34873596
Your code is not complete,

Please see following URL might help you.  Its explain security for file upload and complete method.

http://msdn.microsoft.com/en-us/library/aa479405.aspx

http://www.dotnetspider.com/resources/21345-Uploading-Multiple-files-using-ASP-NET.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx#Y761

Can post complete code?


Good Luck
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34873647
I am trying to upload file and at the same time writing file names to the table. uploading is working fine. I can upload multiple files at the same time but only one record(with file name get inserted in the table). I want to write all file names. pls help me.

thanks

            WMSEntities myEntity2 = new WMSEntities();

              WorkOrderDoc myWorkDocs;
            myWorkDocs = new WorkOrderDoc();

  for (int i = 0; i < uploadedFiles.Count; i++)
                {
                    HttpPostedFile userPostedFile = uploadedFiles[i];

                    if (userPostedFile.ContentLength > 0)
                    {
                        string guidResult = System.Guid.NewGuid().ToString();
                        userPostedFile.SaveAs(filepath + guidResult + extension);

                        myWorkDocs.WorkorderId = cboWOId.SelectedItem.Text;
                        myWorkDocs.Filename = guidResult;
                        myWorkDocs.UploadedBy = User.Identity.Name;
                        myWorkDocs.UploadedOn = DateTime.Now;

                        myEntity2.AddToWorkOrderDocs(myWorkDocs);
                        myEntity2.SaveChanges();
                       
                    }
                       
                }

Open in new window

0
Industry Leaders: 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!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34873761
What is this, "AddToWorkOrderDocs"? A method you created? if so please post.

Which one of the following is the primary key?

  myWorkDocs.WorkorderId
  myWorkDocs.Filename
  myWorkDocs.UploadedBy
  myWorkDocs.UploadedOn
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34874053
primary key is not here bcz it an identity column.

and AddToWorkOrderDocs is a built in function to add records. without this myentity.SaveChanges will not work.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34874107
Please post code for AddToWorkOrderDocs.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34874480
void wmsEntity.AddToWorkOrderDocs(workOrderDoc WorkOrderDoc)
Deprecated method for adding a new object to workOrderDocs entity set.Consider using the .Add method of ObjectSet<T> property set instead.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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