Solved

Entity loop error

Posted on 2011-02-11
7
520 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
Please post code for AddToWorkOrderDocs.
0
 
LVL 7

Author Comment

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl help with selection 14 43
Paging GridView 7 32
Make a border less form movable 2 11
VB.NET HttpWebRequest 12 30
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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

11 Experts available now in Live!

Get 1:1 Help Now