Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
<p>I have below code in my <code>.cs</code> file. This is the sample of code. There are more conditions of <code>if..elseif...else if..</code> etc. To reduce code I attached only sample code with only 2 conditions  if and else..</p>

<p>When I run this module on IIS 7.0 there is error related to connection pooling timeout.. </p>

<p>My question is how to exactly close connection depending on my code's sitution? </p>

<pre><code>public class GenerateSubjectWiseReportDAL
{
    string ConnectionString = "";
    string query = "";
    SqlCommand com;
    SqlConnection con;

    public GenerateSubjectWiseReportDAL()
    {
        ConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
    }

        try
        {
            //  bexcel.Visible = true;
            con = new SqlConnection(ConnectionString);
            con.Open();

            if (sessional.Equals("1"))
            {
                if (lbsubjecttype.Equals("L"))
                {
                    query = "SELECT DISTINCT Attendence_Stu_MainL1.st_id, Attendence_Stu_MainL1.st_rollno, Attendence_Stu_MainL1.st_name, Attendence_Stu_MainL1.scode, Attendence_Stu_MainL1.attendence AS Expr6, " +
                                       " Attendence_Stu_MainL1.total AS Expr7 FROM Attendence_Stu_MainL1 WHERE ((Attendence_Stu_MainL1.sem_no=@semno AND Attendence_Stu_MainL1.scode=@scode) AND Attendence_Stu_MainL1.branch_name=@branchname)  ORDER BY Attendence_Stu_MainL1.scode,Attendence_Stu_MainL1.st_rollno";
                    GridView1.Columns[4].Visible = false;
                    GridView1.Columns[5].Visible = false;
                    GridView1.Columns[8].Visible = false;
                    GridView1.Columns[9].Visible = false;

                    Binding(GridView1, lbsubjecttype);
                }
                else
                {
                    query = "SELECT DISTINCT Attendence_Stu_Main.st_id, Attendence_Stu_Main.st_rollno, Attendence_Stu_Main.st_name, Attendence_Stu_Main.scode, Attendence_Stu_Main.attendence AS Expr4, " +
                                           " Attendence_Stu_Main.total AS Expr5, Attendence_Stu_MainL1.attendence AS Expr6, Attendence_Stu_MainL1.total AS Expr7 FROM  Attendence_Stu_Main LEFT OUTER JOIN " +
                                           " Attendence_Stu_MainL1 ON Attendence_Stu_Main.st_id = Attendence_Stu_MainL1.st_id AND Attendence_Stu_Main.scode = Attendence_Stu_MainL1.scode  WHERE ((Attendence_Stu_Main.sem_no=@semno AND Attendence_Stu_Main.scode=@scode) AND Attendence_Stu_Main.branch_name=@branchname) ORDER BY Attendence_Stu_Main.scode,Attendence_Stu_Main.st_rollno";

                    GridView1.Columns[8].Visible = false;
                    GridView1.Columns[9].Visible = false;

                    Binding(GridView1, lbsubjecttype);
                }
            }
         }
      }
      catch (SqlException ee)
      {
         lbmsg.Text = ee.Message;
         lbmsg.Visible = true;
         lbmsg.ForeColor = Color.Green;
         throw ee;
      }
      finally
      {
          con.Close();
      }
    }

    public void Binding(GridView mygridview, string subjtype)
    {
        try
        {
            int n = 0;
            com = new SqlCommand(query, con);
            //Initialize your connection using sqlConnection object
            SqlDataAdapter da = new SqlDataAdapter(com);
            com.Parameters.AddWithValue("@semno",semno);
            com.Parameters.AddWithValue("@scode", code);
            com.Parameters.AddWithValue("@branchname", branch);

            //Since it's a SELECT statement, the result may be a collection of tables so collect the result in a dataset like this

            DataSet ds = new DataSet();
            da.Fill(ds, "ss");

            com.ExecuteNonQuery();

            mygridview.DataSource = ds.Tables["ss"]; ;
            mygridview.DataBind();

            foreach (GridViewRow g1 in mygridview.Rows)
            {
                try
                {
                    id = mygridview.Rows[g1.RowIndex].Cells[0].Text;
                    rollno = mygridview.Rows[g1.RowIndex].Cells[1].Text;
                    name = mygridview.Rows[g1.RowIndex].Cells[2].Text;
                    scode = mygridview.Rows[g1.RowIndex].Cells[3].Text;

                    if (subjtype.Equals("L"))
                    {
                        string sql = "INSERT INTO Attendence_Subject_Wise(st_id,st_rollno,st_name,branch_name,sem_no,scode,sess_no,theory,ttotal,lab,ltotal,tutorial,tutotal)"
                              + "VALUES (@Stid, @Strollno,@Stname,@BranchName,@SemNo,@Scode,@Sessno,@theory,@total1,@lab,@total2,@tut,@total3)";

                        con = new SqlConnection(ConnectionString);
                        con.Open();
                        com = new SqlCommand(sql, con);
                        com.Parameters.Add("@Stid", id);
                        com.Parameters.Add("@Strollno", rollno);
                        com.Parameters.Add("@Stname", name);
                        com.Parameters.Add("@BranchName", branch);
                        com.Parameters.Add("@SemNo", semno);
                        com.Parameters.Add("@Scode", scode);
                        com.Parameters.Add("@Sessno", sess);
                        com.Parameters.Add("@theory", n);
                        com.Parameters.Add("@total1", n);
                        com.Parameters.Add("@lab", mygridview.Rows[g1.RowIndex].Cells[6].Text);
                        com.Parameters.Add("@total2", mygridview.Rows[g1.RowIndex].Cells[7].Text);
                        com.Parameters.Add("@tut", n);
                        com.Parameters.Add("@total3", n);

                    }
                    else 
                    {
                        string sql = "INSERT INTO Attendence_Subject_Wise(st_id,st_rollno,st_name,branch_name,sem_no,scode,sess_no,theory,ttotal,lab,ltotal,tutorial,tutotal)"
                                   + "VALUES (@Stid, @Strollno,@Stname,@BranchName,@SemNo,@Scode,@Sessno,@theory,@total1,@lab,@total2,@tut,@total3)";

                        con = new SqlConnection(ConnectionString);
                        con.Open();
                        com = new SqlCommand(sql, con);
                        com.Parameters.Add("@Stid", id);
                        com.Parameters.Add("@Strollno", rollno);
                        com.Parameters.Add("@Stname", name);
                        com.Parameters.Add("@BranchName", branch);
                        com.Parameters.Add("@SemNo", semno);
                        com.Parameters.Add("@Scode", scode);
                        com.Parameters.Add("@Sessno", sess);
                        com.Parameters.Add("@theory", mygridview.Rows[g1.RowIndex].Cells[4].Text);
                        com.Parameters.Add("@total1", mygridview.Rows[g1.RowIndex].Cells[5].Text);
                        com.Parameters.Add("@lab", mygridview.Rows[g1.RowIndex].Cells[6].Text);
                        com.Parameters.Add("@total2", mygridview.Rows[g1.RowIndex].Cells[7].Text);
                        com.Parameters.Add("@tut", mygridview.Rows[g1.RowIndex].Cells[8].Text);
                        com.Parameters.Add("@total3", mygridview.Rows[g1.RowIndex].Cells[9].Text);
                    }

                    com.ExecuteNonQuery();
                    con.Close();
                }
                catch (SqlException ex)
                {
                    lbmsg.Text = ex.Message;
                    lbmsg.Visible = true;
                    lbmsg.ForeColor = Color.Green;
                }
            }
        }
        catch (SqlException ex)
        {
            lbmsg.Text = ex.Message;
            lbmsg.Visible = true;
            lbmsg.ForeColor = Color.Green;
        }
    }
}
</code></pre>
