简体   繁体   中英

cannot access a disposed object exception in my c# winform

I have a simple c# winform which use report viewer to display reports . . my form is like below `

    private void member_Search_Load(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Maximized;
        if (key1 == "index")
        {
            try
            {
                this.report_membersTableAdapter.Fill(this.DataSet2.report_members, key1, "", value1, DateTime.Now, DateTime.Now);
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex) {
                MessageBox.Show("mbr_srh::ind" + ex.Message);
            }
        }
        if (key1 == "name")
        {
            try
            {
                this.report_membersTableAdapter.Fill(this.DataSet2.report_members, key1, value1, "", DateTime.Now, DateTime.Now);
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex) {
                MessageBox.Show("mbr_srh::nam"+ex.Message);
            }
        }
        if (key1 == "library")
        {
            try
            {
                this.report_membersTableAdapter.Fill(this.DataSet2.report_members, key1, "", "", d11, d22);
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex) {
                MessageBox.Show("mbr_srh::lib" + ex.Message);
            }
        }
        if (key1 == "school")
        {
            try
            {
                this.report_membersTableAdapter.Fill(this.DataSet2.report_members, key1, "", "", d11, d22);
                this.reportViewer1.RefreshReport();

            }
            catch (Exception ex) {
                MessageBox.Show("mbr_srh::sch" + ex.Message);
            }
        }


    }

    public void set(string key, string value)
    {
        key1 = key;
        value1 = value;
        this.Show();
    }
    public void Setdate(string key, DateTime d1, DateTime d2)
    {
        key1 = key;
        d11 = d1;
        d22 = d2;
        this.Show();
    }

    private void reportViewer1_Load(object sender, EventArgs e)
    {

    }


}

}`

i can first time to call the set() or setdate() functions but when i call the second time to above functions ,it gave me a exception cannot access a disposed object exception:object name:member_search with highlighting this.Show() in set() or setdate() functions ,, can anyone gove me the solution for this?

If you close a Form it gets disposed.

  1. Start debugging (F5)
  2. Set a breakpoint (F9) the beginning of your method member_Search_Load
  3. Step through each line (F10) and investigate the values of each used variable

As far as i can see you do open your report form from your main form. Then view your reports and close your report form. Even if you hold a variable for your report form in the main form your report form gets disposed when you close it by clicking the cross. So you have to start by creating a new report form before you want to show it the next time.

If you want to avoid this you have to use member_SearchForm.Hide().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM