尝试datareader很接近,但我从不关闭这些读者

时间:2017-01-29 06:51:06

标签: c# asp.net winforms

我开发了C#应用程序,我在程序中关闭了读取器,cekprevnext中的读者是错误的,它说"无效的尝试读取,当读取器关闭时#34;,所以这就是我的所有代码:

private void cekprevnext()
    {
        ambilsoal();
        con.Open();
        MySqlCommand cmds = new MySqlCommand("Select * from jawab_soal WHere SoalID='"+SOALID+"'", con);
        MySqlDataReader drs = cmds.ExecuteReader();
        if (drs.HasRows)
        {
            while (drs.Read()) // ==> This attempt reader close
            {
                jwbsudah = drs["jambilsoal"].ToString();
                if (jwbA.Text == jwbsudah)
                {
                    jwbA.Checked = true;
                }
                else if (jwbB.Text == jwbsudah)
                {
                    jwbB.Checked = true;
                }
                else if (jwbC.Text == jwbsudah)
                {
                    jwbC.Checked = true;
                }
                else
                {
                    jwbD.Checked = true;
                }
            }
        }  
        con.Close();
    }

这是我的ambil soal方法:

private void ambilsoal()
    {
        jwbA.Checked = false;
        jwbB.Checked = false;
        jwbC.Checked = false;
        jwbD.Checked = false;
        if (count == jml)
        {
            btnnext.Enabled = false;
            btnprev.Enabled = true;
        }
        else if (count == 1)
        {
            btnprev.Enabled = false;
            btnnext.Enabled = true;
        }
        else
        {
            btnnext.Enabled = true;
            btnprev.Enabled = true;
        }
        try
        {
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * From soal Where KatID='" + KatID + "' AND NoSoal='"+count+"'", con);
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                soalTampil.Text = dr["Pertanyaan"].ToString();
                jwbA.Text = dr["A"].ToString();
                jwbB.Text = dr["B"].ToString();
                jwbC.Text = dr["C"].ToString();
                jwbD.Text = dr["D"].ToString();
                SOALID = dr["SoalID"].ToString();
                jwbbelum = dr["Jawaban"].ToString();
            }
            con.Close();
        }
        catch (Exception ex)
        {
            con.Close();
            RadMessageBox.Show(ex.Message.ToString());
        }
    }
某人可以帮忙吗?

0 个答案:

没有答案