连接未关闭,连接的当前状态已打开 [英] The connection was not closed, The connection's current state is open
本文介绍了连接未关闭,连接的当前状态已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
try
{
if (HttpContext.Current.Request.Cookies["CRegistrationID"].Value != "")
{
if (FUImages.HasFile && FUBrochure.HasFile)
{
string CateName = ddlCreateAddCategory.SelectedItem.ToString();
string ArName = ddlCreateAddArea.SelectedItem.ToString();
Con.Open();
string uid = HttpContext.Current.Request.Cookies["CRegistrationID"].Value;
HttpContext.Current.Response.Cookies["Image"].Value = CateName;
HttpContext.Current.Response.Cookies["Upload"].Value = ArName;
ImageAutoNum = Guid.NewGuid().ToString();
ImageAutoNum = ImageAutoNum.Substring(0, 3);
FUImages.SaveAs(Server.MapPath("~/ConsumerImages/" + ImageAutoNum + FUImages.FileName));
ImageUrl = "~/ConsumerImages/" + ImageAutoNum + FUImages.FileName;
ImageAutoNum2 = Guid.NewGuid().ToString();
ImageAutoNum2 = ImageAutoNum2.Substring(0, 3);
FUBrochure.SaveAs(Server.MapPath("~/ConsumerImages/" + ImageAutoNum2 + FUBrochure.FileName));
FUBImages = "~/ConsumerImages/" + ImageAutoNum2 + FUBrochure.FileName;
Cmd = new SqlCommand("Insert into ConsumerAdvertiesment(CategoryName,ADshop_name,ADShop_Address,ADshop_city,ADShop_mob,ADshop_Email,RegistrationId,startddate,endddate,ImageName,Description,ImagePath,BImageName,BImagePath,Status) values('" + CateName + "','" + txtshopname.Text + "','" + txtCadress.Text + "','" + txtCity.Text + "','" + txtcontact.Text + "','" + txtemail.Text + "','" + uid + "','" + txtstart.Text + "','" + txtEnd.Text + "','" + ImageUrl + "','" + txtDesc.Text + "','" + ImageUrl + "','" + FUBImages + "','" + FUBImages + "','Active')", Con);
Cmd.ExecuteNonQuery();
Response.Redirect("Success.aspx?Pop=Adv");
Con.Close();
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please Select Image & Brochure');", true);
}
}
else
{
Response.Cookies["LoginRedirect"].Value = "Yes";
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please Log in First !!! ');", true);
}
}
catch (Exception ex)
{ }
finally
{
Con.Close();
}
推荐答案
如果你遇到这个问题你应该关闭现有的开放连接前,打开连接之前
行之前
con.Open();
下单代码
if(con.ConnectionState == ConnectionState.Open)
{
Con.close();
}
它应该可以解决你的问题,
Hi,
if you get that problem you should close the existing open connection, before opening connection
before the line
con.Open();
place the code
if(con.ConnectionState == ConnectionState.Open)
{
Con.close();
}
it should solve your problem,
if (con.State == ConnectionState.Open)
{
Con.Close();
}
不共享连接对象。您可以在app config或web config上保留连接字符串。
当您需要进行数据库操作时,请创建新连接并在完成后对其进行处理。以下代码示例将对您有所帮助。
Don't Share the Connection Object. you can keep connection string on app config or web config.
When you need to do a database operation, create new connection and dispose it after you done with it. following code sample will help you.
using (SqlConnection conn = new SqlConnection(YourConnectionString))
using (SqlCommand cmd = new SqlCommand(SqlString, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
}
这篇关于连接未关闭,连接的当前状态已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文