By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Cannot access a disposed object. Object name: 'frmMsg'.

P: 127
Hi All,

I'm developing a windows desktop solution using VC# , I deal with my database using Dataset.

My Problem that i'm trying to call a Form to display a confirmation message after every Save, Delete ...., I wanted to make my own Message box, it works well but for one time only, if i tried to call it more than one time it gives me this exception:

<Cannot access a disposed object.
Object name: 'frmMsg'.>

My Code:

Expand|Select|Wrap|Line Numbers
  1. public partial class frmOrgHierarchy : Form
  2.     {
  3.         // Create object from the Message Form
  4.         frmMsg objMsg;
  6.         public frmOrgHierarchy()
  7.         {
  8.             InitializeComponent();
  9.             objMsg = new frmMsg();
  10.         }
  12.         private void btnSaveSector_Click(object sender, EventArgs e)
  13.         {
  14.             try
  15.             {
  16.                 objMsg.Show();
  18.                 this.Validate();
  19.                 if (sectorNameTextBox.Text.Equals("") ||  
  20.                     sectorNameTextBox.Text.Equals(null))
  21.                 {
  22.                     objMsg.processMessages("برجاء إدخال إسم القطاع", "err");
  23.                 }
  24.                 else
  25.                 {
  26.                     this.hR_OrgSectorsTableBindingSource.EndEdit();
  27.                     this.hR_OrgSectorsTableTableAdapter.Update(
  28.                     this.systemDS.HR_OrgSectorsTable);
  29.                     hR_OrgSectorsTableBindingNavigator.Enabled = true;
  30.                    objMsg.processMessages("تم حفظ البيانات !!", "ok");
  31.                 }
  32.             }
  33.             catch (Exception ex)
  34.             {
  35. //The Exception is being cached here
  36.                 objMsg.processMessages(ex.Message.ToString(), "err");
  37.             }
  38.         }

Actually I don't even understand why the object has been disposed while the form is still running !!

Need help.
Dec 17 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 221
I can't see any problems with the bit of code you've posted but somewhere along the line frmMsg is being disposed of.

What happens in the objMsg.processMessages() method? Have you checked that it's not being accidentally disposed of in there?
Dec 17 '08 #2

P: 127
Thanks fro your reply,
The problem was here :

private void btnOK_Click(object sender, EventArgs e)

This is the "OK" button in the message form, i replaced it with this.Hide();

i discovered this after i took my break :)
Dec 17 '08 #3

Expert 100+
P: 692

You can solve the error using the following way :
After doing Save,Edit and Delete you should create the object of the form you want to show. i.e.
objMsg = new frmMsg();
Currently it is created in the constructor frmOrgHierarchy() . Instead of this if you create the form object after Save operation , you will not get disposed form error because new form object will be created.
Dec 17 '08 #4

Expert 5K+
P: 7,872
I believe that calling .Close() on a form object also call the destructor (by way of calling Dispose())

Instead of .Close(), you could try using .Hide() and .Show() to hide/show the form.
Just remember to change what is displayed on it.
Dec 17 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.