473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

JustRun
127 100+
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;
  5.  
  6.         public frmOrgHierarchy()
  7.         {
  8.             InitializeComponent();
  9.             objMsg = new frmMsg();
  10.         }
  11.  
  12.         private void btnSaveSector_Click(object sender, EventArgs e)
  13.         {
  14.             try
  15.             {
  16.                 objMsg.Show();
  17.  
  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.         }
  39.  

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

Need help.
Dec 17 '08 #1
4 10309
nukefusion
221 Expert 100+
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
JustRun
127 100+
Thanks fro your reply,
The problem was here :

private void btnOK_Click(object 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
Dec 17 '08 #3
shweta123
692 Expert 512MB
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.
Dec 17 '08 #4
Plater
7,872 Expert 4TB
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

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

Similar topics

2
by: salih ataoz | last post by:
i close a form and i want to open it again it show me Cannot access a disposed object at vb6 this is ok at comman1_click form1.show
7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
5
by: D Witherspoon | last post by:
I use the following code to open up a form. ------------------------------------------------------- If fImage Is Nothing Then fImage = New frmImage End If fImage.Show()
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
5
by: mthgk | last post by:
I have a C# MDI app. The child forms do alot of work, so this work is perfomed on a different thread created using ThreadPool.QueueUserWorkItem(). Because the status of the work is important to...
3
by: Tracey | last post by:
Hi, there I met a very wierd problem. My startup form say Form1 has a datagrid say datagrid1, when I click an item in datagrid1, I wanna show form2. In my code, I achieved that with: private sub...
3
by: Tracey | last post by:
sorry I post this problem again. I have to stop my work to fix the problem. I'm doing a multi form application(Not a MDI one). My startup form say Form1 has a datagrid say datagrid1, when I...
2
by: Rajat Tandon | last post by:
Hi, I have a grid which is continuously updating by the data from a external event. When I close the form on which the grid is placed, then it gives the error message ... "Can not access a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.