ara
ara

Reputation: 11

SqlException was unhandled by user code during execution

Can someone explain to me what I am doing wrong here? The error shows that I have an error at con.Open();

Thanks in advance

public partial class registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection (@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\phonebook.mdf;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }

        con.Open();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Insert into registration values('"+ firstname.Text +"', '"+ lastname.Text +"', '"+ email.Text +"', '"+ username.Text +"', '"+ password.Text +"', '"+ contact.Text +"')";
        cmd.ExecuteNonQuery();

        Response.Write("<script>alert('record inserted successfully');</script>");
    }
}

Upvotes: 1

Views: 69

Answers (0)

Related Questions