System.InvalidOperationException:读取器关闭时无效尝试调用Read

时间:2015-10-29 12:00:12

标签: c#

我的以下代码中出现以下错误:

  

读取器关闭时无效尝试调用Read。

我测试了我的代码时间,但我没有发现代码中的错误。谁能告诉我我的代码在哪里不正确或者为什么会出现这个错误?

   /*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 admission_no=@admission_no and sub_id=@sub_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 admission_no=@admission_no and sub_id=@sub_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 admission_no=@admission_no and sub_id=@sub_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());

2 个答案:

答案 0 :(得分:2)

问题在于:

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

您已在读者阅读之前关闭了连接。只有在连接打开时,阅读器才会读取。

答案 1 :(得分:1)

您正在关闭此行的读者:

dr.Close();

然后四行,你这样做:

if (dr.Read())

错误信息非常清楚:关闭后无法使用阅读器。

请注意,这是您正在使用的dr第二个实例,而不是第一个 - 它是您在if块中创建的实例,其中包含注释:< / p>

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

我的建议是,你要稍微清理你的代码,以减少导致问题的混乱,并使其更难找到。此外,您应该将the using statement与实现一次性模式的类一起使用,例如System.Data.SqlClient.SqlConnectionSystem.Data.SqlClient.SqlCommandSystem.Data.SqlClient.SqlDataReader。例如,这是编写与数据读取器交互的代码的简洁方法:

using (var conn = new System.Data.SqlClient.SqlConnection(usingConnectionString))
{
    using (var command = new System.Data.SqlClient.SqlCommand(usingQuery, conn))
    {
        using (var reader = command.ExecuteReader())
        {
            // do your work here...
        }
    }
}

请注意,我不会致电Close,因为一旦代码存在using范围,就会自动完成。