Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created tournament and have got 1 image for each of the tournament somewhere else and stored them in the db. Now, when people click on "Delete", it should get their tournament details and respective image deleted in a single connection.

But it's showing error "There is already an open DataReader associated with this Command which must be closed first."


C#
protected void LVTournament_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            if (Session["TournId"] != null)
            {
                int ClubId = Convert.ToInt32(Request.Cookies["ClubDetails"]["ClubId"]);
                int TournId = Convert.ToInt32(Session["TournId"]);
                SqlConnection con = new SqlConnection(constr);
                ImageDeletion(TournId);
                string querydelTour = "DELETE FROM Tournaments WHERE TournamentId = @TournId";
                string querydelimg = "DELETE FROM TournamentImages WHERE TournamentId = @TournId";
                SqlCommand cmd1 = new SqlCommand(querydelTour, con);
                SqlCommand cmd2 = new SqlCommand(querydelimg, con);
                cmd1.Parameters.AddWithValue("@TournId", TournId);
                cmd2.Parameters.AddWithValue("@TournId", TournId);
                con.Open();
                cmd1.ExecuteNonQuery();
                cmd2.ExecuteNonQuery();
                con.Close();
                UpdateTourCount(ClubId);
                ClientScript.RegisterStartupScript(Page.GetType(), "Success", "<script language='javascript'>alert('Tournament Deleted...')</script>");
                Response.Redirect("TournamentView.aspx");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "Popup", "SessionExpires();", true);
                return;
            }
        }


What I have tried:

I guess, there is a problem over here.

C#
con.Open();
                cmd1.ExecuteNonQuery();
                cmd2.ExecuteNonQuery();
                con.Close();


How to get rid of this?
Posted
Updated 9-Oct-16 22:11pm

C#
SqlConnection con = new SqlConnection(constr);
ImageDeletion(TournId);
string querydelTour = "DELETE FROM Tournaments WHERE TournamentId = @TournId";
string querydelimg = "DELETE FROM TournamentImages WHERE TournamentId = @TournId";
// these two commands use the same connection so you can only process
// one of the commands at a time, but you're doing both.
SqlCommand cmd1 = new SqlCommand(querydelTour, con);
SqlCommand cmd2 = new SqlCommand(querydelimg, con);


C#
SqlConnection con1 = new SqlConnection(constr);
SqlConnection con2 = new SqlConnection(constr);
ImageDeletion(TournId);
string querydelTour = "DELETE FROM Tournaments WHERE TournamentId = @TournId";
string querydelimg = "DELETE FROM TournamentImages WHERE TournamentId = @TournId";
// these two commands use the same connection so you can only process
// one of the commands at a time, but you're doing both.
SqlCommand cmd1 = new SqlCommand(querydelTour, con1);
SqlCommand cmd2 = new SqlCommand(querydelimg, con2);


You'll also need con1.Open and con2.Open etc.

However a better design would be to make a function like "DeleteTournamentImages" and "DeleteTournament" and inside those functions you'd do the SQL work related to just that task, then you'd call both of those functions.
 
Share this answer
 
What has happened is that you are refusing an SQL connection, which you don't close and SqlCommand, SqlDataReader, etc. objects that you don't Dispose when you are finished with them.
So somewhere in your code you create a DataReader, and it still exists, even if it was created 'local' to a method because it is on the heap where it will remain until the Garbage Collector gets around to Disposing of it.

Don't recycle your connection, make a new one each time. Always Dispose connection, command, and all other SQL objects, or this kind of problem is not only going to get worse, it will also start to crash your website as it gets busy: these things are a scarce resources and you can't just keep creating them!
 
Share this answer
 
Comments
Bigprey 10-Oct-16 2:55am    
I am not much of a coder. So, will the problem solved if I use con.Dispose() instead of Con.Close()?

con.Open();
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
con.Dispose();

Among 30 other pages of the same site, there is no SQL Connection coming except here. So, I don't guess, there are any disclosed connection from other pages.

Thanks in advance and have a nice day ahead.
OriginalGriff 10-Oct-16 4:07am    
No - read what I said and look at the *rest* of your code!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900