我有以下錯誤在我下面的代碼: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());
下一次,請先在Google中搜索您的問題。 –
作爲一個方面說明,你應該重構重複代碼(例如嵌套的'if')到一個方法,該方法獲得百分比並返回一個等級,也許''GetGrade(int mark);' – christiandev