this error actually occurs in two different places withing my web application.
This event fires in code behind file when a user clicks insert link button.
Line of code that fails is below
context.tblUPCCodes.AddObject(upcProfile);
protected void UpcInsertLinkButton_Click(object sender, EventArgs e){
try
{
// Create an entity type
upcProfile = new tblUPCCode();
// Set to false on insert
upcProfile.Obsolete = false;
// Set to empty
upcProfile.Wh_ID = String.Empty;
// Set properties for UPC entity
upcProfile.RTD = cboxRTD.Checked;
upcProfile.Export = cboxExport.Checked;
upcProfile.COORequired = cboxCOO.Checked;
upcProfile.UPCCode = tboxUpcProfileCode.Text.Trim();
upcProfile.AltUPC = tboxUpcProfileAlternateCode.Text.Trim();
upcProfile.GlobalUPC = tboxUpcProfileGlobalNum.Text.Trim();
upcProfile.ShelfLife = Convert.ToInt32(tboxUpcProfileShelfLife.Text.Trim());
upcProfile.ForeignFlavorName = tboxUpcProfileForiegnFlavName.Text.Trim();
upcProfile.Barcode = tboxUpcProfileBarcode.Text.Trim();
// Get primary keys of related entities
CompanyProfilePK = Convert.ToInt32(ddlCompanies.SelectedValue);
BottleProfilePK = Convert.ToInt32(ddlBottles.SelectedValue);
FormulaProfilePK = Convert.ToInt32(ddlFormulas.SelectedValue);
FillerProfilePK = Convert.ToInt32(ddlFillers.SelectedValue);
PalletizerProfilePK = Convert.ToInt32(ddlPalletizers.SelectedValue);
LabelerProfilePK = Convert.ToInt32(ddlLabelers.SelectedValue);
CaseformerProfilePK = Convert.ToInt32(ddlCaseFormers.SelectedValue);
upcProfile.CompanyID = CompanyProfilePK;
upcProfile.BottleTypeID = BottleProfilePK;
upcProfile.FormulaID = FormulaProfilePK;
upcProfile.FillerProfileID = FillerProfilePK;
upcProfile.PalletizerProfileID = PalletizerProfilePK;
upcProfile.LabelerProfileID = LabelerProfilePK;
upcProfile.CaseFormerProfileID = CaseformerProfilePK;
using (var context = new ProductSpecSheetsEntities()){
// Object Set adds entity type to entity
context.tblUPCCodes.AddObject(upcProfile);
// Context Object save entity and inserts new row in DB table
context.SaveChanges();
// Now we can extract the primary key for the inserted record from SQL DB
UpcProfilePK = upcProfile.UpcID;
}
}
catch (Exception exception) {
throw exception.InnerException;
}
ocsJack
Member
27 Points
31 Posts
Error An item with the same key has already been added.
May 04, 2012 07:34 PM|LINK
this error actually occurs in two different places withing my web application.
This event fires in code behind file when a user clicks insert link button.
Line of code that fails is below
context.tblUPCCodes.AddObject(upcProfile);
protected void UpcInsertLinkButton_Click(object sender, EventArgs e){ try { // Create an entity type upcProfile = new tblUPCCode(); // Set to false on insert upcProfile.Obsolete = false; // Set to empty upcProfile.Wh_ID = String.Empty; // Set properties for UPC entity upcProfile.RTD = cboxRTD.Checked; upcProfile.Export = cboxExport.Checked; upcProfile.COORequired = cboxCOO.Checked; upcProfile.UPCCode = tboxUpcProfileCode.Text.Trim(); upcProfile.AltUPC = tboxUpcProfileAlternateCode.Text.Trim(); upcProfile.GlobalUPC = tboxUpcProfileGlobalNum.Text.Trim(); upcProfile.ShelfLife = Convert.ToInt32(tboxUpcProfileShelfLife.Text.Trim()); upcProfile.ForeignFlavorName = tboxUpcProfileForiegnFlavName.Text.Trim(); upcProfile.Barcode = tboxUpcProfileBarcode.Text.Trim(); // Get primary keys of related entities CompanyProfilePK = Convert.ToInt32(ddlCompanies.SelectedValue); BottleProfilePK = Convert.ToInt32(ddlBottles.SelectedValue); FormulaProfilePK = Convert.ToInt32(ddlFormulas.SelectedValue); FillerProfilePK = Convert.ToInt32(ddlFillers.SelectedValue); PalletizerProfilePK = Convert.ToInt32(ddlPalletizers.SelectedValue); LabelerProfilePK = Convert.ToInt32(ddlLabelers.SelectedValue); CaseformerProfilePK = Convert.ToInt32(ddlCaseFormers.SelectedValue); upcProfile.CompanyID = CompanyProfilePK; upcProfile.BottleTypeID = BottleProfilePK; upcProfile.FormulaID = FormulaProfilePK; upcProfile.FillerProfileID = FillerProfilePK; upcProfile.PalletizerProfileID = PalletizerProfilePK; upcProfile.LabelerProfileID = LabelerProfilePK; upcProfile.CaseFormerProfileID = CaseformerProfilePK; using (var context = new ProductSpecSheetsEntities()){ // Object Set adds entity type to entity context.tblUPCCodes.AddObject(upcProfile); // Context Object save entity and inserts new row in DB table context.SaveChanges(); // Now we can extract the primary key for the inserted record from SQL DB UpcProfilePK = upcProfile.UpcID; } } catch (Exception exception) { throw exception.InnerException; }EntityFramework