enter image description here连接未关闭。连接的当前状态已打开。 //新
公共部分类注册:System.Web.UI.Page { 的SqlConnection CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [ “dbcon”]的ToString());
public int chkuser()
{
if (con.State.ToString() == "open")
con.Close();
SqlCommand cmd = new SqlCommand("select count(*) from Task2_SignUp where UserName= '"+txtUName.Text+"'",con);
con.Open();
int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());
return flag;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSignUp_Click(object sender, EventArgs e)
{
if(chkuser()==0)
{
if (con.State.ToString() == "open")
con.Close();
SqlCommand cmd = new SqlCommand("insert into Task2_SignUp (UserName,Password,Name) values (@UserName, @Password, @Name)", con);
cmd.Parameters.AddWithValue("@Username", txtUName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
con.Open(); //ERROR SHOWING IN THIS LINE
cmd.ExecuteNonQuery();
con.Close();
lblSignUp.Text = "Registration Successfull";
_blank();
}
else if(chkuser()>0)
{
lblSignUp.Text = "Username not available";
}
}
public void _blank()
{
txtName.Text = "";
txtUName.Text = "";
txtPassword.Text = "";
}
protected void btnNext_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
}
}
问:这是我收到不知道为什么..和suprprisingly相同的代码工作两年星期前,但现在不工作的错误..请尽可能帮助我。
不知道它是否会产生差异,但我会使用以下if(connection.State == ConnectionState.Open)而不是当前检查的方式来查看连接是否打开 –
可能的连接重复没有关闭连接的当前状态是打开](https://stackoverflow.com/questions/13343236/the-connection-was-not-closed-the-connections-current-state-is-open) –
没有..相同问题::( – Saakey7