For simple validations, you should be using Javascript since it does not require a postback to the server.
To answer your question, the reason why the code on the
Button1_Click
is still running is because you have not checked the value of the
Page.IsValid
property. You might want to add it on your code, like this:
protected void Button1_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
try
{
string constr;
constr = WebConfigurationManager.ConnectionStrings["JAPITConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
string str = "insert into Doe_detail values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
Label2.Visible = true;
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
catch (Exception es)
{
}
}
}