Solved

Entity loop error

Posted on 2011-02-11
7
524 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 300 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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