Unhandled Exception when exiting app using dispose method

K

kuhrty

Hi,

I am creating a multi-winform project. Everything has been working
fine, except after I perform an update to the database and decide to
Exit the winform after the update operation is complete.

I have error handling in my code but not on the dispose method. I call
the dispose method exiting the application using the code below but
results in this error message
"An unhandled exception of type 'System.NullReferenceException'
occurred in system.management.dll
Additional information: Object reference not set to an instance of an
object."

I am may be using the dispose method incorrectly but I can't seem to
locate the issue. Any advice or hints is greatly appreaciated.

private void mnuExit_Click(object sender, System.EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?","Exit",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Dispose();
}
}

protected override void Dispose( bool disposing )
{
if (!bOK)
{
if (MessageBox.Show("You have not updated the current data. Would you
like to before exiting?", "Exit",MessageBoxButtons.YesNo ) ==
DialogResult.Yes)
{
return;
}
}
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

Thanks in advance

Mark
 
G

Guest

Why don't you just use the .Close method of your form to quit the
application, and handle the Closing event to check for unsaved data?

As far as I know (but I could be wrong) you only need to dispose WinFOrms
that were used as modal dialog boxes, not for modeless forms.

Hope this helps,

Joris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top