ObjectDisposedException

When I am calling Form2 from From1 by clicking the button, first time it is showing the data. Next time when I click the button, its throwing the following error..

Caught: Cannot access a disposed object named "Form2".
Object name: "Form2".


Code:
void button1_Click(object sender,system.EventArgs e)
{
  try
  {  form2.Show();
  }catch (ObjectDisposedException err)
   {Console.WriteLine("Caught: {0}", err.Message);}
}


I have tried the ShowDialog method also. In that case it is appending the same data again.

When I click the button from Form1, it shows 4 records in Form2 (in datagrid). When I click it again it appends the same records. so I see 8 records.

Pl suggest.

regds
Rajesh
rnavduriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Volcano_88101Commented:
What is the Definition of Form2?
Is Form2 the name of your Form?
If you are probably want to define your variable something like this

private Form2 myform = new Form2();

in your Form1 class
Then whenever you call your ShowCode go
myform.Show();


Also on another note.
You might change the OnClosing and have it autohide on the user hitting the okay button or hitting the Close button

0
Nebulus_Commented:
from .NET Framework Class Library:

Note:   When the Close method is called on a Form displayed as a modeless window, you cannot call the Show method to make the form visible, because the form's resources have already been released. To hide a form and then make it visible, use the Control.Hide method.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.