0

What is wrong with my code? Pls Help. I keep getting error System.Threading.ThreadAbortException: Thread was being aborted.

This is my registration page c# code as below:

    protected void submitbtn_Click(object sender, EventArgs e)
    {
        try
        {
            con.ConnectionString = "Data Source=DESKTOP-H7KQUT1;Initial Catalog=SAOS;Integrated Security=True";
            con.Open();
            string insertQuery = "insert into account" + "(username,password) values (@username,@password)";
            SqlCommand cmd = new SqlCommand(insertQuery, con);
            cmd.Parameters.AddWithValue("@username", TextBoxUN.Text);
            cmd.Parameters.AddWithValue("@password", TextBoxPass.Text);
            cmd.ExecuteNonQuery();

            string insertQuery1 = "insert into parent" + "(Email,Contact,FName,LName,HomeAddress,Gender) values (@Email,@Contact,@FName,@LName,@HomeAddress,@Gender)";
            SqlCommand cmd1 = new SqlCommand(insertQuery1, con);
            cmd1.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
            cmd1.Parameters.AddWithValue("@Contact", TextBoxContact.Text);
            cmd1.Parameters.AddWithValue("@FName", TextBoxFName.Text);
            cmd1.Parameters.AddWithValue("@LName", TextBoxLName.Text);
            cmd1.Parameters.AddWithValue("@HomeAddress", TextBoxHome.Text);
            cmd1.Parameters.AddWithValue("@Gender", DropDownListGender.SelectedItem.ToString());
            cmd1.ExecuteNonQuery();

            MessageBox.Show("Registration is successfull!");
            Response.Redirect("Login.aspx");
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error:" + ex.ToString());
        }
    }
}
Michael
  • 21
  • 1
  • 6

1 Answers1

0

There is already an answer here: Why Response.Redirect causes System.Threading.ThreadAbortException?

This is caused by your Response.Redirect.

Also, it might be better to use:

using(SqlCommand cmd = new SqlCommand(insertQuery, conn) { // The sql command code here like parameters, etc. }

Using dispose immediatly the SqlCommand. It might prevent error since you are using multiple SqlCommand.