关闭所有连接并仍然“已经有一个与此命令关联的打开的datareader必须先关闭” [英] Closed all connections and still getting “there is already an open datareader associated with this command which must be closed first”

查看:108
本文介绍了关闭所有连接并仍然“已经有一个与此命令关联的打开的datareader必须先关闭”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在尝试删除锦标赛时遇到上述错误的代码。由于锦标赛与图像相关联,因此在删除整个锦标赛之前,代码首先调用ImageDeletion函数然后调用锦标赛计数函数。



This is my code for which I am getting the above said error when trying to delete the tournament. Since Tournament is associated with an image, the code calls the ImageDeletion function first and then the tournament count function before deleting the whole tournament.

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 con1 = new SqlConnection(constr);
       SqlConnection con2 = new SqlConnection(constr);
       ImageDeletion(TournId);
       string querydelTour = "DELETE FROM Tournaments WHERE TournamentId = @TournId";
       SqlCommand cmd1 = new SqlCommand(querydelTour, con1);
       cmd1.Parameters.AddWithValue("@TournId", TournId);
       con1.Open();
       cmd1.ExecuteNonQuery();
       con1.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;
      }
 }





图像删除功能是





The Image Deletion function is

private void ImageDeletion(int TournId)
  {
     SqlConnection con = new SqlConnection(constr);
     SqlCommand cmdchk = new SqlCommand("SELECT count(*) Valid FROM TournamentImages WHERE  TournamentId= @TournId");
     cmdchk.Connection = con;
     cmdchk.Parameters.AddWithValue("@TournId", TournId);
     con.Open();
     int Count = (int)cmdchk.ExecuteScalar();
     for (int i = 0; i <= Count; i++)
        {
          string query = "SELECT ImagePath FROM TournamentImages WHERE TournamentId = @TournId";
          SqlCommand cmddel = new SqlCommand(query, con);
          cmddel.Parameters.AddWithValue("@TournId", TournId);
          SqlDataReader reader = cmddel.ExecuteReader();
          while (reader.Read())
            {
              string FilePath = reader[0] as string;
              string path = Server.MapPath(FilePath);
              FileInfo file = new FileInfo(path);
                if (file.Exists)
                  {
                     file.Delete();
                   }
             }
          }
        con.Close();
     }





比赛计数功能是





And the tournament count function is

protected void UpdateTourCount(int ClubId)
 {
  SqlConnection con = new SqlConnection(constr);
  string Query = "UPDATE TournamentCount SET Count = Count + 1 WHERE ClubId =@ClubId";
  SqlCommand cmd = new SqlCommand(Query, con);
  cmd.Parameters.AddWithValue("@ClubId", ClubId);
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
  }





我关闭了当时和那里的连接,但我仍然收到此错误。



请提及我哪里弄错了。提前致谢



我的尝试:



我试过了为每个连接提供不同的名称以及



I have close the connections then and there and still I am getting this error.

Kindly mention me where am I making the mistake. Thanks in advance

What I have tried:

I have tried giving each connection with different names and also

MultipleActiveResultSets=True;





它们都不起作用。

.

None of them are working.

推荐答案

尝试在 ImageDeletion():

Try closing your data reader in ImageDeletion():
...
while (reader.Read())
  {
    string FilePath = reader[0] as string;
    string path = Server.MapPath(FilePath);
    FileInfo file = new FileInfo(path);
      if (file.Exists)
        {
           file.Delete();
         }
   }
reader.Close();// <- add this line
...


这篇关于关闭所有连接并仍然“已经有一个与此命令关联的打开的datareader必须先关闭”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆