title: c# ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
body: <p>can some one find that why i am getting this error ? i have marked it where i am getting the error</p>
  
  <pre><code>public string ExportRecords(string query, string sheetname)
      {
          string filename = "";
          DataSet ds = new DataSet("New_DataSet");
          DataTable dt = new DataTable(sheetname);
  
          ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
          dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
  
          SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["KMFConnectionString"].ToString());
          con.Open();
  
          string sql = query;
          SqlCommand cmd = new SqlCommand(sql, con);
          SqlDataAdapter adptr = new SqlDataAdapter();
  
          adptr.SelectCommand = cmd;
          adptr.Fill(dt);
          con.Close();
  
          ds.Tables.Add(dt);
  
          string connstr = connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + orgrepository.OrganizationMetaValueByKey("KMFFileDownloadPath") + filename + "; Extended Properties=Excel 8.0";
  
  
          OleDbConnection connection = new OleDbConnection(connstr);
  
  
          using (OleDbCommand commands = connection.CreateCommand())
          {
              commands.CommandText = "CREATE TABLE [Sheet20] (F1 number, F2 char(255), F3 char(128))";
              commands.ExecuteNonQuery();     ****getting error here****
              for (int i = 1; i &lt;= 20; i++)
              {
  
                  commands.CommandText = "INSERT INTO [Sheet20] (F1, F2, F3) VALUES(1,\"Fake Record\",\"Fake Record\")";
                  commands.ExecuteNonQuery();
              }
          }
  
          if (dt.Rows.Count &gt; 0)
          {
              //filename = sheetname + DateTime.Today.Day.ToString() + DateTime.Today.Month.ToString() + DateTime.Today.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".xlsx";
              filename = sheetname + "-output" + ".xls";                
              ExcelLibrary.DataSetHelper.CreateWorkbook(orgrepository.OrganizationMetaValueByKey("KMFFileDownloadPath") + filename, ds);
  
  
              //    connection.Close();
  
         }
          return filename;
      }
  </code></pre>