I am batch adding a bunch of records from an internal system to our web database server (eBilling type data). I have a problem where if there is a data error (seperate issue), every add after that errors out.
foreach (InvoiceHeader invoiceHeader in invoiceHeaders)
{
rrn++;
db.AddToInvoiceHeaders(invoiceHeader);
if (rrn >= RECORDS_AT_A_TIME)
{
try
{
db.SaveChanges();
rrn = 0;
}
catch (Exception e)
{
string errorText = "Error in blah blah blah \n\n";
errorText += "Error: " + e.ToString();
Log.Error(errorText);
// Delete out bad record or entire set of records here.
}
}
}
So the first 500 may go good, then I might get an invalid date. Nothing else gets added to the server and the logs get slammed with errors and crashes.
How can I either clear out the one record in error OR just clear out that batch of records? So it can finish properly and we can go back and fix the errors thus adding back the missing data.
Update: I want to be able to just log the error, remove the problem record(s) and move on. In this case it happens to be a date issue, I am not looking for solving this one instance, but all future unknown issues. How do I do the // Delete out bad record or entire set of records here.?
This is probably a less than ideal solution, but what seems to work for what I need to move on.