Unhandled exception during switching child forms

VAer-4038 776 Reputation points
2021-01-11T04:27:07.35+00:00

Backend database: Access file. If I do the following four steps, it ends up with unhandled exception, why? It seems that second time opening ChildForm2 connection causing the error… But ChildForm2 already closes at below step 3.

  1. When application opens, ChildForm1 is default opening form.
  2. When I click parent form's menutrip button to open ChildForm2, it runs fine, DataGridView can show the query result.
  3. When I click parent form's menutrip button to open ChildForm1 again, it runs fine.
  4. When I click parent form's menutrip button to open ChildForm2 again, it crushes with unhandled exception(see screenshot). I set the breaking point and debug, then step into code line by line, it crushes at Cn.Open. ChildForm2_Load works fine at step 2.

Thanks.
Edit: It also happens if I click parent form's menutrip button to open ChildForm2 again immediately after step 2, no need to switch to another child form.

System.Runtime.InteropServices.SEHException  
  HResult=0x80004005  
  Message=External component has thrown an exception.  
  Source=System.Data  
  StackTrace:  
   at System.Data.Common.UnsafeNativeMethods.SQLDriverConnectW(OdbcConnectionHandle hdbc, IntPtr hwnd, String connectionstring, Int16 cbConnectionstring, IntPtr connectionstringout, Int16 cbConnectionstringoutMax, Int16& cbConnectionstringout, Int16 fDriverCompletion)  
   at System.Data.Odbc.OdbcConnectionHandle.Connect(String connectionString)  
   at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle)  
   at System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions)  
   at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)  
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)  
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)  
   at System.Data.Odbc.OdbcConnection.Open()  

55382-second-cn-open.jpg

55406-error-details.jpg

private void OpenChildForm1ToolStripMenuItem_Click(object sender, EventArgs e)  
{  
    if (ActiveMdiChild != null)  
        ActiveMdiChild.Close();  
  
    FChildForm1 fChildForm1 = new FChildForm1();  
    fChildForm1.MdiParent = this;  
    fChildForm1.WindowState = FormWindowState.Maximized;  
    fChildForm1.Show();  
}  
  
  
private void OpenChildForm2ToolStripMenuItem_Click(object sender, EventArgs e)  
{  
    if (ActiveMdiChild != null)  
        ActiveMdiChild.Close();  
  
    FChildForm2 fChildForm2 = new FChildForm2();  
    fChildForm2.MdiParent = this;  
    fChildForm2.WindowState = FormWindowState.Maximized;  
    fChildForm2.Show();  
}  
  
  
  
private void FChildForm1_Load(object sender, EventArgs e)  
{       
    DateTime timeOfDayGreeting = DateTime.Now;  
    lblDate.Text = "Today is " + DateTime.Now.ToString("MMM d, yyyy");  
}  
  
  
  
private void FChildForm2_Load(object sender, EventArgs e)  
{  
    string sqlStr = "SELECT Username, FirstName, LastName from TableUser";  
    DataTable dt = new DataTable();  
    using (OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString))  
    {  
        //if the connection is not already open - then open it  
        if (Cn.State != ConnectionState.Open)  
        {  
            Cn.Open();  
        }  
  
        using (OdbcDataAdapter adapt = new OdbcDataAdapter(sqlStr, Cn))  
        {  
            adapt.Fill(dt);  
            dgvABC.DataSource = dt;  
        }  
    }  
  
}  

  
  
Developer technologies | Windows Forms
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.