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