Try this:
protected void Button1_Click(object sender, System.EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from Exam_master", con);
if (con.State == ConnectionState.Closed) {
con.Open();
}
dr = cmd.ExecuteReader();
while (dr.Read()) {
if (dr("Stud_id") == TextBox1.Text) {
Label16.Text = "";
Label8.Text = dr("Stud_id");
Label10.Text = dr("Course_id");
Label9.Text = dr("Name");
Label14.Text = dr("Class");
Label11.Text = dr("Maximum");
Label12.Text = dr("Minimum");
Label13.Text = dr("Total");
break;
} else {
Label16.Text = "Invalid Student......";
Label8.Text = ("-------------");
Label10.Text = ("-------------");
Label9.Text = ("-------------");
Label14.Text = ("-------------");
Label11.Text = ("-------------");
Label12.Text = ("-------------");
Label13.Text = ("-------------");
}
}
}
hope it helps :)