Hi All,
I'm new to C# and I'm trying to make an application that will import an excel file into a linq list which in turn will import that to a database. On the form itself i have a 2 buttons, Import and Cancel (the cancel button is for the background worker) and 4 datagridview. When you click the import button for the first time, it will run perfectly, everything is written into the database. but if you cancel, or import a new file, I get an error message:
The property 'ID' is part of the object's key information and cannot be modified.
I figured that the items in either the datagridview, list, or the objData4 still contains the items from the previous import, so I have a line of code that will clear everything from all sources, objData, list, datagridview, datasource, but i still get that error. I've been at this for almost 2 weeks now. if anyone can help me it would be so amazing!!!
Thanks in Advance
if you need to see the code, please let me know.
Update 6.27.11
I un-commented out the following lines, to ensure that everything was cleared before importing a new file:
itemMasterList.Clear();
objData1.Clear();
but then i get this new error at the dmConsumption.SaveChanges(); line:
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
im totally at a loss, can someone please help me out?
heres the code ^^
#region Excel Import Button
private void button1_Click(object sender, EventArgs e)
{
pBarStat.Value = 0;
dgView1.DataSource = null;
#region Delete Statements
foreach (var deleteMe in dmConsumption.SalesLines)
{
dmConsumption.SalesLines.DeleteObject(deleteMe);
}
foreach (var deleteMe in dmConsumption.PurchaseLines)
{
dmConsumption.PurchaseLines.DeleteObject(deleteMe);
}
foreach (var deleteMe in dmConsumption.ItemLedgers)
{
dmConsumption.ItemLedgers.DeleteObject(deleteMe);
}
foreach (var deleteMe in dmConsumption.ItemMasters)
{
dmConsumption.ItemMasters.DeleteObject(deleteMe);
}
dmConsumption.SaveChanges();
#endregion
try
{
openFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFile.Title = "Import Excel File From:";
openFile.FileName = "";
openFile.Filter = "Excel 97-2003|*.xls";
if (openFile.ShowDialog() == DialogResult.OK)
{
source = openFile.FileName;
if (IMWorker.IsBusy)
{
button1.Enabled = false;
Status.Text = "Cancelling...";
IMWorker.CancelAsync();
}
else
{
button1.Enabled = false;
button3.Enabled = true;
button3.Visible = true;
Status.Text = "Processing data...";
IMWorker.RunWorkerAsync();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
#region IMWorker
#region IMWorker DoWork
public void IMworker_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000);
excelPop = ExcelConnection();
objDA1.SelectCommand = excelPop[0];
objDA1.Fill(objData1);
increment = objData1.Tables[0].Rows.Count;
pBarStat.ProgressBar.Invoke((MethodInvoker)delegate()
{
pBarStat.Maximum = increment;
});
if (dgView1.InvokeRequired)
{
dgView1.Invoke(new MethodInvoker(delegate { dgView1.DataSource = objData1.Tables[0].DefaultView; }));
}
foreach (DataRow row in objData1.Tables[0].Rows)
{
var IM = new itemMaster();
try
{
IM.ID = row["No#"].ToString();
}
catch (Exception ex)
{
IM.IMError = "Error";
}
try
{
IM.name1 = row["Description"].ToString();
}
catch (Exception ex)
{
IM.IMError = "Error";
}
try
{
IM.name2 = row["Description 2"].ToString();
}
catch (Exception ex)
{
IM.IMError = "Error";
}
try
{
IM.unitPrice = double.Parse(row["Unit Cost"].ToString());
}
catch (Exception ex)
{
IM.unitPrice = 0;
IM.IMError = "Error";
}
itemMasterList.Add(IM);
pBarStat.ProgressBar.Invoke((MethodInvoker)delegate()
{
pBarStat.Value = increment;
});
}
foreach (var imItem in itemMasterList)
{
ItemMaster newItemMaster = new ItemMaster();
newItemMaster.ID = imItem.ID.ToString();
newItemMaster.Generic = imItem.name1.ToString();
newItemMaster.Brand = imItem.name2.ToString();
newItemMaster.UnitPrice = decimal.Parse(imItem.unitPrice.ToString());
dmConsumption.ItemMasters.AddObject(newItemMaster);
}
dmConsumption.SaveChanges();
}
#endregion
#region IMWorker Complete
private void IMWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
button3.Visible = false;
button3.Enabled = false;
button1.Enabled = true;
button1.Visible = true;
if (e.Cancelled)
{
MessageBox.Show("Operation Cancelled", "Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Status.Text = "Ready...";
return;
}
else
{
Status.Text = "Operation Successfull!";
pBarStat.Value = increment;
}
}
#endregion