title: Connection is open and is not closed in asp.net
body: <p>Hello Friends i am trying to develop a shout box in asp.net but it ends with an error The connection was not closed. The connection's current state is open. Can you please help me out what is the problem in my code .</p>
  
  <pre><code>Shout.aspx.cs
  
  using System.Data;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Text;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace finalWork
  {
      public partial class Shout : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  PopulateTextBox();
              }
          }
          private void PopulateTextBox()
          {
              SqlConnection con = ConnectionManager.GetDatabaseConnection();
              DataTable dt = new DataTable();
              string name = string.Empty, message = string.Empty;
              StringBuilder sb = new StringBuilder(string.Empty);
              try
              {
                  con.Open();
                  string sqlStatement = "SELECT * FROM Message";
                  SqlCommand cmd = new SqlCommand(sqlStatement, con);
                  SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
                  sqlDa.Fill(dt);
                  if (dt.Rows.Count &gt; 0)
                  {
                      for (int i = 0; i &lt; dt.Rows.Count; i++)
                      {
                          //get the data stored from the DataTable
                          name = dt.Rows[i]["Username"].ToString();// where Name is the FieldName from database
                          message = dt.Rows[i]["Message"].ToString();
                          sb.AppendFormat("Name:{0}Date Posted:{1}{2}", name + Environment.NewLine
                                                 , DateTime.Now.ToShortDateString() + Environment.NewLine
                                                 , message + Environment.NewLine);
                      }
                      // get the concated and formatted values from string builder and display the result in TextBoxPrintMessage
  
                      TextBoxPrintMessage.Text = sb.ToString();
                  }
                  con.Close();
              }
              catch (System.Data.SqlClient.SqlException ex)
              {
                  string msg = "Fetch Error:";
                  msg += ex.Message;
                  throw new Exception(msg);
              }
              finally
              {
                  con.Close();
              }
          }
  
          private void AddNewPost(string name, string message)
          {
  
              SqlConnection con = ConnectionManager.GetDatabaseConnection();
              string sqlStatement = string.Empty;
  
              sqlStatement = "INSERT INTO Message" +
                             "(Username,Message)" +
                             "VALUES (@Username,@Message)";
              try
              {
                  con.Open();
                  SqlCommand cmd = new SqlCommand(sqlStatement, con);
                  cmd.Parameters.AddWithValue("@Username", name);
                  cmd.Parameters.AddWithValue("@Message", message);
                  cmd.CommandType = CommandType.Text;
                  cmd.ExecuteNonQuery();
              }
              catch (System.Data.SqlClient.SqlException ex)
              {
                  string msg = "Insert Error:";
                  msg += ex.Message;
                  throw new Exception(msg);
              }
              finally
              {
                  con.Close();
              }
          }
  
          protected void ButtonPost_Click(object sender, EventArgs e)
          {
              // Check for empty values fieds before inserting the record
              if (TextBoxName.Text != string.Empty &amp;&amp; TextBoxMessage.Text != string.Empty)
              {
                  //insert new post to database
                  AddNewPost(TextBoxName.Text.Trim(), TextBoxMessage.Text.Trim());
                  //Populate the TextBox to reflect changes made
                  PopulateTextBox();
              }
              else
              {
                  //display message if the field was not supplied
                  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Script",
                  "alert('Please supply the required  fields!');", true);
              }
          }
  
  
  
      }
  } 
  </code></pre>