数据库双重查询错误

数据库双重查询错误

本文介绍了数据库双重查询错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行代码

"There is already an open DataReader associated with this Command which must be closed first"


protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
	SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=RoomReservation;Integrated Security=True");
	con.Open();

	SqlCommand cmdsel = new SqlCommand("Select Payment from Bank where  NICNO ='MOH'", con);
	SqlDataReader read = cmdsel.ExecuteReader();

	while (read.Read())
	{
		if ( int.Parse(tprice.Text) <int.parse(>                {
			SqlCommand cmd = new SqlCommand("insert into romreserve values('" + Label1.Text + "','" + Title.SelectedItem.Text + "','" + FN.Text + "','" + SN.Text + "','" + Email.Text + "','" + Address.Text + "','" + Mobile.Text + "','" + Phone.Text + "','" + Country.Text + "','" + City.Text + "','" + rname.Text + "','" + cid.Text + "','" + cod.Text + "','" + adult.Text + "','" + child.Text + "','" + nr.Text + "','" + rent.Text + "','" + nrr.Text + "','" + tprice.Text + "','false') " + "UPDATE Bank SET Payment = (Payment -" + tprice.Text + ") WHERE NICNO = '" + FN.Text + "'", con);
			cmd.ExecuteNonQuery();
		}
		else {
			string script = "alert('Your Account balance will be less than');";
			ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
		}
	}
}

推荐答案


 Declare another connection object and use it in the while loop

SqlConnection con1 = new SqlConnection("Data Source=(local);Initial Catalog=RoomReservation;Integrated Security=True");


while (read.Read())
    {
        if ( int.Parse(tprice.Text) <int.parse(>                {
            SqlCommand cmd=new SqlCommand("...",con1)
              con1.Open();
cmd.ExecuteNonQuery();

con1.Close()
}


<appSettings>
        <add key="dbConnection" value="Data Source=accerlap2; Initial Catalog=PMSW; User ID=sa; Password=p@ssword;"/>
    </appSettings>




2.并在aspx.cs页面中




2.and in aspx.cs page

SqlConnection connection = new SqlConnection(WebConfigurationManager.AppSettings["dbConnection"].ToString());


SqlCommand cmd = new SqlCommand("Select * from table1...., connection);



希望它能解决.如果是您的解决方案,请选择接受解决方案..



hope it works.if it is ur solution select accept solution..


这篇关于数据库双重查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:25