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
Solved

Entity loop error

Posted on 2011-02-11
7
523 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
  • 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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