0

Possible Duplicate:
Cannot add an entity with a key that is already in use (LINQ)

I have googled a lot on this issue and found many people facing the same issue. but could get through it yet. many solutions were posted but no gain.

I have written a LINQ query which retrieve some 38 records and using foreach loop i insert those records in other table as per the code.

var Results =  (from p in (from d in compact.CURRENT_STATUS
                                                   where QtrEndDate >= d.ValidStartDate && QtrEndDate <= d.ValidDueDate && d.LocationCode == EmployeeLocation && d.DeptCode == selecteddept
                                            select new
                                            {
                                                loccode = d.LocationCode,
                                                taskcode = d.TaskCode,
                                                deptcode = d.DeptCode,
                                                flag = d.Flag,
                                                startdate = d.ValidStartDate,
                                                duedate = d.ValidDueDate,
                                                remarks = d.Remarks,
                                                content = d.UploadDocContent,
                                                title = d.UploadDocTitle,
                                                type = d.UploadDocType
                                            }).Union(from i in compact.HISTORY_STATUS
                                                     where QtrEndDate >= i.ValidStartDate && QtrEndDate <= i.ValidEndDate && i.LocationCode == EmployeeLocation && i.DeptCode ==  selecteddept
                                                     select new
                                                     {
                                                loccode = i.LocationCode,
                                                taskcode = i.TaskCode,
                                                deptcode = i.DeptCode,
                                                flag = i.Flag,
                                                startdate = i.ValidStartDate,
                                                duedate = i.ValidEndDate.Date,
                                                remarks = i.Remarks,
                                                content = i.UploadDocContent,
                                                title = i.UploadDocTitle,
                                                type = i.UploadDocType
                                                     }) select p);





                        foreach (var item in Results)
                        {
                            REPORT_TABLE RT = new REPORT_TABLE();
                            {

                                RT.LocationCode = item.loccode;
                                RT.TaskCode = item.taskcode;
                                RT.DeptCode = item.deptcode;
                                RT.Flag = item.flag;
                                RT.ValidStartDate = item.startdate;
                                RT.ValidDueDate = item.duedate;
                                RT.Remarks = item.remarks;
                                RT.UploadDocContent = item.content;
                                RT.UploadDocTitle = item.title;
                                RT.UploadDocType = item.type;
                                RT.SubmittedBy = (Session["username"]).ToString();
                                RT.SubmittedByIP = (Session["IP"]).ToString();
                                RT.SubmittedOn = System.DateTime.Now;
                                RT.ReportID = RHqueryFind.ReportID;
                                RT.AsOnDate = QtrEndDate;
                            }
                            compact.REPORT_TABLEs.InsertOnSubmit(RT);  
                            compact.SubmitChanges();
                        };

But i do get the error Cannot add an entity with a key that is already in use in the last line i.e SubmitChanges.

In one of the solution i found a line of code to be added as RefreshMode but this could not solve my problem.

Community
  • 1
  • 1
Rahul2788
  • 189
  • 1
  • 3
  • 10
  • What is the primary key field in `REPORT_TABLE`? You either do not assign it at all, or you assign it a value, that some other row (already existing in the database) has. – horgh Jan 30 '13 at 04:37
  • Also see [Cannot add an entity with a key that is already in use](http://stackoverflow.com/questions/3720594/cannot-add-an-entity-with-a-key-that-is-already-in-use) – horgh Jan 30 '13 at 04:38

1 Answers1

1

I think you have to remove Primary key of your column. Create new column i.e. SerialNo and set it as primary key with Identity Specification.

I got same problem i.e. Cannot add an entity with a key that is already in use but when I removed the primary key of column which I was inserting the value and create a new column with Primary key and Identity Specification. Problem solved

Ajay
  • 6,418
  • 18
  • 79
  • 130
  • Thanks!! there was an issue with the primary key. the same records were getting inserted again. so the error came. Thanks All!!! – Rahul2788 Jan 30 '13 at 06:26
  • Hey Ajay....can u please help me out again. I have a Gridview which display some data. i want to display the data of gridview on some button control in pdf file in browser itself..basically to render contents into pdf format in same browser. I Have written the following code but unable to get it. – Rahul2788 Jan 31 '13 at 06:13
  • `GVReportData.AllowPaging = false; GVReportData.DataBind(); Response.ClearContent(); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GVReportData.RenderControl(htw); Response.ContentType = "application/Pdf"; Response.AddHeader("content-disposition",htw.ToString()); Response.AddHeader("content-length", htw.ToString()); Response.Write(sw.ToString()); Response.End();` – Rahul2788 Jan 31 '13 at 06:14
  • I am unable to post question due to some reason. PLS help me out. i dont want to give download option which i am able to do, but to directly open the data of gridview in pdf format in browser itself. – Rahul2788 Jan 31 '13 at 06:16
  • @Rahul2788 I will check for this. – Ajay Jan 31 '13 at 11:43
  • @Rahul2788 this might help you not sure http://stackoverflow.com/questions/4242389/pdf-render-to-asp-net-web-page-results-in-static-title – Ajay Jan 31 '13 at 12:25