Hi
I get the following error in my Web app
"There is already an open DataReader associated with this Command which must be closed first"
Is this because my DB code doesnt have a close method or a using block?
Thanks
Steven
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
EhteshamRank #3,979 · 374 reputationPosted May 23, 2012, 2:48 AM EDT
MyDataReaderObject.Close();
Guest UserRank #4,868 · 321 reputationPosted May 23, 2012, 2:57 AM EDT
Each method which accesses the DB has a using block which I thought automatically disposes when finished?
Guest UserRank #4,868 · 321 reputationPosted May 23, 2012, 3:41 AM EDT
List
var orderId = usersShoppingCart.SubmitOrder(User.Identity.Name, out orderDetails);
int totalQuantity = 0;
foreach (OrderDetail od in orderDetails)
{
totalQuantity += od.Quantity;
}
Its as if there needs to be a closing or end after the foreach or even a pause before the next part of the method gets called, which in this case redirects to PayPal.
Thanks
Steven
Guest UserRank #4,868 · 321 reputationPosted May 23, 2012, 3:49 AM EDT
Guest UserRank #4,868 · 321 reputationPosted May 23, 2012, 4:25 AM EDT
After reading some forums it is suggested I retrieve all my results\return values first, store in a list\collection and then do something with that list?
EhteshamRank #3,979 · 374 reputationPosted May 23, 2012, 4:29 AM EDT
Your error message says it
if you have used datareader with a connection and left datareader open and trying to use the same connection you will get that error
Check you code again, their must be a datareader
Guest UserRank #4,868 · 321 reputationPosted May 23, 2012, 4:47 AM EDT
After some investigation it turned out I needed to add the MARS (Multiple Active Result Sets) to the Web.Config and set the value to True.
All works fine!
And NO I dont have a Datareader declared explicity - Im using EF.
EhteshamRank #3,979 · 374 reputationPosted May 23, 2012, 4:50 AM EDT