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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustRun
    New Member
    • Mar 2008
    • 127

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

    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:

    Code:
    public partial class frmOrgHierarchy : Form
        {
            // Create object from the Message Form
            frmMsg objMsg;
    
            public frmOrgHierarchy()
            {
                InitializeComponent();
                objMsg = new frmMsg();
            }
    
            private void btnSaveSector_Click(object sender, EventArgs e)
            {
                try
                {
                    objMsg.Show();
                    
                    this.Validate();
                    if (sectorNameTextBox.Text.Equals("") ||  
                        sectorNameTextBox.Text.Equals(null))
                    {
                        objMsg.processMessages("برجاء إدخال إسم القطاع", "err");
                    }
                    else
                    {
                        this.hR_OrgSectorsTableBindingSource.EndEdit();
                        this.hR_OrgSectorsTableTableAdapter.Update(
                        this.systemDS.HR_OrgSectorsTable);
                        hR_OrgSectorsTableBindingNavigator.Enabled = true;
                       objMsg.processMessages("تم حفظ البيانات !!", "ok");
                    }
                }
                catch (Exception ex)
                {
    //The Exception is being cached here
                    objMsg.processMessages(ex.Message.ToString(), "err");
                }
            }

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

    Need help.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    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.processM essages() method? Have you checked that it's not being accidentally disposed of in there?

    Comment

    • JustRun
      New Member
      • Mar 2008
      • 127

      #3
      Thanks fro your reply,
      The problem was here :

      private void btnOK_Click(obj ect sender, EventArgs e)
      {
      this.Close();
      }

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

      i discovered this after i took my break :)
      Thanks

      Comment

      • shweta123
        Recognized Expert Contributor
        • Nov 2006
        • 692

        #4
        Hi,

        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.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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.

          Comment

          Working...