这里已经是一个与此命令关联的开放DataReader,必须先关闭它

时间:2013-07-06 15:44:36

标签: asp.net c#-4.0 ado.net

protected void cmdLogin_Click(object sender,EventArgs e)     {

    Class1.con = CreateConnection();
    string str = "SELECT Ad_Regid from Admin_Profile WHERE Ad_Regid LIKE @TempPasscode";
    cmd = new SqlCommand(str, Class1.con);
    if(Class1.con.State==ConnectionState.Closed)
        Class1.con.Open();
        cmd.Parameters.AddWithValue("@TempPasscode", txtcurrentpwd.Text.Trim());
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            if (dr[0].ToString() == txtcurrentpwd.Text)
        {
                using (cmd = new SqlCommand("_insertAdminLogin", Class1.con))
                if (Class1.con.State == ConnectionState.Closed)
                Class1.con.Open();
                cmd.CommandType = System.Data.CommandType.StoredProcedure ;
                cmd.Parameters.AddWithValue("@AdminType", ddltype.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@AdminUsrname", txtuname.Text);
                cmd.Parameters.AddWithValue("@AdminPwd", txtconfirmpwd.Text);

                **using cmd.ExecuteNonQuery();**
         }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Warning", "javascript:alert('Temporary Password Does not Match!');", true);
            }
         }
        Class1.con.Close();
}    

我正在尝试保存3个字段,但它显示有一个打开的数据阅读器“你应该关闭它”...但我使用using关键字用于相同的用途....

请帮助!!

1 个答案:

答案 0 :(得分:0)

在调用cmd.ExecunoteNonQuery之前,请调用dr.Close();