2015-10-29 77 views
0

我有以下錯誤在我下面的代碼:System.InvalidOperationException:無效試圖調用讀取時讀取器已關閉

無效試圖調用時讀取器已關閉閱讀。

我測試了我的代碼時間,但是我還沒有找到代碼中的錯誤。任何人都可以告訴我我的代碼不正確或者爲什麼會出現此錯誤?

/*start calculate grade of SA2 */ 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString; 
    if (Convert.ToString(ddlexam.SelectedItem) == "SA2") 
    { 


     int SA2; 
     string grade = null; 
     SqlCommand comm; 
     SqlDataReader dr; 
     con.Open(); 
     comm = new SqlCommand("select SA2 from terms_marks where [email protected]_no and [email protected]_id", con); 
     comm.Parameters.AddWithValue("@sub_id", sub_id); 
     comm.Parameters.AddWithValue("@admission_no", admission_no); 
     dr = comm.ExecuteReader(); 
     while (dr.Read()) 
     { 
      SA2 = Convert.ToInt32(dr["SA2"].ToString()); 
      markspercentage = (SA2 * 100)/maxmarks; 
      if (markspercentage >= 91 && markspercentage <= 100) 
      { 
       grade = "A1"; 
      } 
      else if ((markspercentage >= 81) && (markspercentage <= 90)) 
      { 
       grade = "A2"; 
      } 
      else if (markspercentage >= 71 && markspercentage <= 80) 
      { 
       grade = "B1"; 
      } 
      else if (markspercentage >= 61 && markspercentage <= 70) 
      { 
       grade = "B2"; 
      } 
      else if (markspercentage >= 51 && markspercentage <= 60) 
      { 
       grade = "C1"; 
      } 
      else if (markspercentage >= 41 && markspercentage <= 50) 
      { 
       grade = "C2"; 
      } 
      else if (markspercentage >= 33 && markspercentage <= 40) 
      { 
       grade = "D"; 
      } 
      else if (markspercentage >= 21 && markspercentage <= 32) 
      { 
       grade = "E1"; 
      } 
      else if (markspercentage >= 0 && markspercentage <= 20) 
      { 
       grade = "E2"; 
      } 
     } 
     dr.Close(); 
     cmd = new SqlCommand(); 
     cmd.Connection = con; 
     cmd.CommandText = "CreategradeDetails"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@st_id", st_id); 
     cmd.Parameters.AddWithValue("@roll_no", roll_no); 
     cmd.Parameters.AddWithValue("@admission_no", admission_no); 
     cmd.Parameters.AddWithValue("@sub_id", sub_id); 

     cmd.Parameters.AddWithValue("@FA1", DBNull.Value); 
     cmd.Parameters.AddWithValue("@FA2", DBNull.Value); 
     cmd.Parameters.AddWithValue("@SA1", DBNull.Value); 
     cmd.Parameters.AddWithValue("@Total_FirstTerm", DBNull.Value); 
     cmd.Parameters.AddWithValue("@FA3", DBNull.Value); 
     cmd.Parameters.AddWithValue("@FA4", DBNull.Value); 
     cmd.Parameters.AddWithValue("@SA2", grade); 
     cmd.Parameters.AddWithValue("@Total_SecondTerm", DBNull.Value); 
     cmd.Parameters.AddWithValue("@Total_FA", DBNull.Value); 
     cmd.Parameters.AddWithValue("@Total_SA", DBNull.Value); 
     cmd.Parameters.AddWithValue("@GrandTotal", DBNull.Value); 

     result = cmd.ExecuteNonQuery(); 
     con.Close(); 

    } 
    /*end calculate SA2 grade */ 
    /* start calculate Total_SecondTerm grade;*/ 
    if (Convert.ToString(ddlexam.SelectedItem) == "SA2") 
    { 
     int TotalST; 
     string grade = null; 
     SqlCommand comm; 
     SqlDataReader dr; 
     con.Open(); 
     comm = new SqlCommand("select Total_SecondTerm from terms_marks where [email protected]_no and [email protected]_id", con); 
     comm.Parameters.AddWithValue("@sub_id", sub_id); 
     comm.Parameters.AddWithValue("@admission_no", admission_no); 
     dr = comm.ExecuteReader(); 
     if (dr.Read()) 
     { 
      TotalST = Convert.ToInt32(dr["Total_SecondTerm"].ToString()); 
      dr.Close(); 
      comm = new SqlCommand("select Total_SecondTerm from max_marks where [email protected]_no and [email protected]_id", con); 
      comm.Parameters.AddWithValue("@sub_id", sub_id); 
      comm.Parameters.AddWithValue("@admission_no", admission_no); 
      if (dr.Read()) 
      { 
       total_STmaxmarks = Convert.ToInt32(dr["Total_SecondTerm"].ToString()); 
       secondterm_percentage = (TotalST * 100) /total_STmaxmarks; 


      } 
      dr.Close(); 
      if (secondterm_percentage >= 91 && secondterm_percentage <= 100) 
      { 
       grade = "A1"; 
      } 
      else if ((secondterm_percentage >= 81) && (secondterm_percentage <= 90)) 
      { 
       grade = "A2"; 
      } 
      else if (secondterm_percentage >= 71 && secondterm_percentage <= 80) 
      { 
       grade = "B1"; 
      } 
      else if (secondterm_percentage >= 61 && secondterm_percentage <= 70) 
      { 
       grade = "B2"; 
      } 
      else if (secondterm_percentage >= 51 && secondterm_percentage <= 60) 
      { 
       grade = "C1"; 
      } 
      else if (secondterm_percentage >= 41 && secondterm_percentage <= 50) 
      { 
       grade = "C2"; 
      } 
      else if (secondterm_percentage >= 33 && secondterm_percentage <= 40) 
      { 
       grade = "D"; 
      } 
      else if (secondterm_percentage >= 21 && secondterm_percentage <= 32) 
      { 
       grade = "E1"; 
      } 
      else if (secondterm_percentage >= 0 && secondterm_percentage <= 20) 
      { 
       grade = "E2"; 
      } 

     } 

     dr.Close(); 
     cmd = new SqlCommand(); 
     cmd.Connection = con; 
     cmd.CommandText = "CreategradeDetails"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@st_id", st_id); 
     cmd.Parameters.AddWithValue("@roll_no", roll_no); 
     cmd.Parameters.AddWithValue("@admission_no", admission_no); 
     cmd.Parameters.AddWithValue("@sub_id", sub_id); 

     cmd.Parameters.AddWithValue("@FA1", DBNull.Value); 
     cmd.Parameters.AddWithValue("@FA2", DBNull.Value); 
     cmd.Parameters.AddWithValue("@SA1", DBNull.Value); 
     cmd.Parameters.AddWithValue("@Total_FirstTerm", DBNull.Value); 
     cmd.Parameters.AddWithValue("@FA3", DBNull.Value); 
     cmd.Parameters.AddWithValue("@FA4", DBNull.Value); 
     cmd.Parameters.AddWithValue("@SA2", DBNull.Value); 
     cmd.Parameters.AddWithValue("@Total_SecondTerm", grade); 
     cmd.Parameters.AddWithValue("@Total_FA", DBNull.Value); 
     cmd.Parameters.AddWithValue("@Total_SA", DBNull.Value); 
     cmd.Parameters.AddWithValue("@GrandTotal", DBNull.Value); 

     result = cmd.ExecuteNonQuery(); 
     con.Close(); 

    } 
    /* end calculate Total_SecondTerm grade;*/ 

錯誤在這裏上面的代碼

comm.Parameters.AddWithValue("@sub_id",sub_id); 
    comm.Parameters.AddWithValue("@admission_no", admission_no); 
     if(dr.Read())       
     { 
    total_STmaxmarks = Convert.ToInt32(dr["Total_SecondTerm"].ToString()); 
+0

下一次,請先在Google中搜索您的問題。 –

+0

作爲一個方面說明,你應該重構重複代碼(例如嵌套的'if')到一個方法,該方法獲得百分比並返回一個等級,也許''GetGrade(int mark);' – christiandev

回答

1

您關閉讀者在這條線:

dr.Close(); 

那麼四大行後,你這樣做是:

if (dr.Read()) 

錯誤信息很清楚:你關閉後無法使用閱讀器。

注意,這是您正在使用,而不是第一個dr實例 - 它是你的,如果塊內創建實例,你必須註釋:

/*end calculate SA2 grade */ 
/* start calculate Total_SecondTerm grade;*/ 

我的建議將會是你清理你的代碼一點,以減少混亂,導致問題,並使其更難找到它們。此外,您應該使用the using statement以及實現一次性模式的類,如System.Data.SqlClient.SqlConnectionSystem.Data.SqlClient.SqlCommandSystem.Data.SqlClient.SqlDataReader。例如,下面是寫代碼與數據讀取器進行交互的清潔方式:

通知我從來沒有叫Close因爲一旦代碼存在using範圍即會自動完成。

2

問題就在這裏:

dr.Close(); //Here 
cmd = new SqlCommand(); 
cmd.Connection = con; 
cmd.CommandText = "CreategradeDetails"; 

您已經關閉了連接的閱讀器讀取它。只有在連接打開時,閱讀器纔會讀取。

+0

謝謝你的回覆問題就解決了 – user2342574

相關問題