2016-11-08 15 views
0

這是我在嘗試刪除錦標賽時遇到上述錯誤的代碼。由於錦標賽與圖像相關聯,因此在刪除整個錦標賽之前,代碼首先調用ImageDeletion函數,然後調用錦標賽計數函數。關閉所有連接,仍然會出現「已經有一個與此命令關聯的打開的DataReader,必須先關閉」

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; 
     } 
} 

的圖像刪除功能

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(); 
    } 

而且比賽計數功能

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

我有密切的聯繫,然後和那裏,還是我得到這個錯誤。

請問我在哪裏犯錯。在此先感謝

+0

使用*使用*阻止像這樣的問題。希望這可以幫助 –

回答

0

您的錯誤消息應該給你一個小費是怎麼回事。它說:「已經有一個開放的DataReader ...」。所以你已經關閉了連接,但不是讀者。

你應該調用方法Close你已經使用讀卡器後:

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(); 
    } 
} 
reader.Close(); // <- close the reader 

SqlDataReader實現IDisposable,這樣你就可以alternativelly使用using語句來實現相同的結果:

using(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(); 
     } 
    } 
} 
+0

它現在工作正常...我剛剛插入「reader.Close();」圖像插入功能。謝謝你,小夥伴 –

相關問題