2017-06-15 131 views
0

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相同的代码工作两年星期前,但现在不工作的错误..请尽可能帮助我。

+0

不知道它是否会产生差异,但我会使用以下if(connection.State == ConnectionState.Open)而不是当前检查的方式来查看连接是否打开 –

+0

可能的连接重复没有关闭连接的当前状态是打开](https://stackoverflow.com/questions/13343236/the-connection-was-not-closed-the-connections-current-state-is-open) –

+0

没有..相同问题::( – Saakey7

回答

0

该问题已解决。这是一个小错误。而不是(con.State.ToString()==“打开”)我写了(con.State.ToString()==“open”)...意味着代替资本'O'我写了小'o'..因此这个问题发生..休息我的代码是abosultely罚款。作为一名培训生开发人员,我学到了这个东西。如果还有其他像我一样刚开始学习的dot net的实习开发人员,那么你一定不要犹豫。只要记住语法。

0

问题:

  1. 功能chkUser有一些问题。如果txtUName.Text包含单引号,则会引发异常。最好重新编码以使用参数。

  2. 另外,WHERE是否发生异常?发布堆栈跟踪。

  3. 当您可以使用connection.State == ConnectionState.Open时,不需要con.State.ToString() == "open"

  4. 我们不知道您的数据库自“两周前”以来是否发生了变化,因此您可能从此处开始。

+0

1)这个错误是否连接到数据库? 2)在第二次运行时发生在函数Chkuser中的问题..我用断点作弊。3)我总是使用con.state.tostring()==“open”,就像我的培训师所教导的那样..我仍然处于培训期。会有什么问题吗? :) – Saakey7

相关问题