protected void Button8_Click(object sender, EventArgs e) { //First code CL.search("select CorrectAnswer from CoursesTBL where CourseID= " + (int.Parse(CoursesDropList.SelectedValue.ToString())) + " and rowID=" + int.Parse(HiddenText.Text).ToString() + ""); if (CL.dr.HasRows) { CompareAnsTextBox.Text = CL.dr["CorrectAnswer"].ToString(); } //second code CL.cmd.Parameters.Clear(); CL.Operatios_SP("STDET_Insert"); CL.cmd.Parameters.AddWithValue("@STDID", int.Parse(STDIDText.Text.ToString())); CL.cmd.Parameters.AddWithValue("@Question", LabelQuestion.Text.ToString()); CL.cmd.Parameters.AddWithValue("@Answer", int.Parse(HiddenTextRB.Text.ToString())); CL.cmd.Parameters.AddWithValue("@CorrAnswer", int.Parse(CompareAnsTextBox.Text.ToString())); CL.cmd.Parameters.AddWithValue("@rowID", int.Parse(HiddenText.Text.ToString())); CL.cmd.Parameters.AddWithValue("@CourseID", int.Parse(CoursesDropList.SelectedValue.ToString())); CL.cmd.ExecuteNonQuery(); CL.cmd.Parameters.Clear(); CL.conn.Close(); //Third code CL.SingleSearch("select count(rowID) from CoursesTBL WHERE CourseID=" + int.Parse(CoursesDropList.SelectedValue.ToString()) + "", CountText); CL.SingleSearch("select count(Answer) from STDETTBL WHERE STDID=" + int.Parse(STDIDText.Text.ToString()) + " and CourseID=" + int.Parse(CoursesDropList.SelectedValue.ToString()) + "", STDCountText); PercentageLabel.Text = ((float.Parse(STDCountText.Text) / float.Parse(CountText.Text)) * 100).ToString(); } And these are my procedures //Search for the first code public void search(string s) { try { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); SqlCommand cmd = new SqlCommand(s, conn); dr = cmd.ExecuteReader(); dr.Read(); } catch { conn.Close(); } } //save for the second code public void Operatios_SP(string Sql) { try { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); cmd.Connection = conn; cmd.CommandText = Sql; cmd.CommandType = CommandType.StoredProcedure; } catch { conn.Close(); } } //Search for the third code public void SingleSearch(string s, TextBox Value) { try { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); cmd.Connection = conn; cmd.CommandText = s; Value.Text = cmd.ExecuteScalar().ToString(); } catch { Value.Text = null; } finally { conn.Close(); } } So could you guys help me find out where the problem is What I have tried: I have tried to close the connection sometimes from the button and sometimes from the procedurs.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)