0

enter image description here

public partial class SignUp : System.Web.UI.Page { SqlConnection con = new 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");
    }
}

}

QUESTION : This is the error i am getting dont know why.. and suprprisingly this same code worked two weeks back but not working now.. please help me if possible.

Saakey7
  • 17
  • 2
  • 7
  • Not sure if it will make a differance but I would use the following if (connection.State == ConnectionState.Open) instead of the current way you are checking to see if the connection is open – Kevin Kunderman Jun 15 '17 at 16:34
  • Possible duplicate of [The connection was not closed the connection's current state is open](https://stackoverflow.com/questions/13343236/the-connection-was-not-closed-the-connections-current-state-is-open) – Kevin Kunderman Jun 15 '17 at 16:39
  • No.. same issue.. :( – Saakey7 Jun 15 '17 at 16:47

2 Answers2

0

Issues:

  1. Function chkUser has an issue. If the txtUName.Text contains a single quote, it will throw an exception. Best to re-code it to use a parameter.

  2. Also, WHERE is the exception occurring? Post a stack trace.

  3. There's no need for con.State.ToString() == "open" when you could use connection.State == ConnectionState.Open.

  4. We have no visibility as to whether your database has changed since 'two weeks ago', so you might start there.

Xavier J
  • 4,326
  • 1
  • 14
  • 25
  • 1) is this error connected to database? 2) problem occuring in Function Chkuser during second time run.. i cheked with breakpoint.. 3) i always use con.state.tostring()=="open" as taught by my trainer.. am still in training period.. will it be any issue ? :) – Saakey7 Jun 16 '17 at 04:08
0

The issue has been solved. it's a minor error. Instead of (con.State.ToString() == "Open") i wrote (con.State.ToString() == "open") ... means instead of Capital 'O' i wrote small 'o' .. hence this problem occured.. rest my code is abosultely fine. As a trainee developer i learned this thing. If there is any other trainee developer of dot net who just started like me then you must not hesitate.. Just remember about the syntax.

Saakey7
  • 17
  • 2
  • 7