I'm having some problems with DataContexts using linq to sql for an asp.net c# web application.
I first had problems with exceptions being thrown as I didn't dispose of the DataContext, same errors as in this question. I was using a static DataContext which was probably why it wasn't disposed of properly, and after reading this and several other articles I wrapped all calls in using statements to be sure they'll get disposed of.
However, now I got problems when I need to update a foreign key as the DataContext has already been disposed of. I'm not sure why it's been disposed of already, and what the best practice would be to do in this scenario so any ideas would be greatly appreciated!
Short example here:
UPDATE: I think my example was too confusing when I tried to make it as short as possible so here's a longer and hopefully better example:
private static void SendTexts(List<TextAlert> TextQueue)
{
using (THTDataContext db = new THTDataContext())
{
foreach (TextAlert text in TextQueue)
{
try
{
// do IntelliSMS stuff
// set status to 'sent'
text.Status = 1;
db.SubmitChanges();
}
catch (IntelliSMSException ex)
{
// set status to 'failed'
text.Status = 2;
db.SubmitChanges();
}
}
}
}
Thanks,
Annelie