locked
Cannot access a disposed object. Object name : "Preview" RRS feed

  • Question

  • Hi All,

    i am trying to show  the excel file using preview button.

    when i did first time...it is opened fine...

    If try to open second time ...it is giving error like Like:Cannot access a disposed object.   Object name : "Preview"

    Let me know where it is causing error..Any suggestions would be appreciated 


    kavitha
    Monday, January 23, 2012 6:39 AM

Answers

  • Hi Kavitha,

    Was the Preview class a form which has a datagridview on it? Based on my understanding, you want to select a Excel(through what?) to preview, and once it been selected, and click a button named btnPreview on the main form, the form Preview(which has a DataGridview control on it) will open and the data in the seleced Excel file will be populated to the DataGridView. If I'm misunderstand your question, please do elaborate more clearly about your senario.

    I can't see any problems with the bit of code you've posted but somewhere along the line ibjPreview is being disposed of. 

    To import data from an Excel sheet to a DataGridView control, here is the code:

             private void Button1_Click(System.Object sender, System.EventArgs e)
    	{
    		System.Data.OleDb.OleDbConnection MyConnection = null;
    		System.Data.DataSet DtSet = null;
    		System.Data.OleDb.OleDbDataAdapter MyCommand = null;
    		MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\vb.net-informations.xls';Extended Properties=Excel 8.0;");
    		MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
    		MyCommand.TableMappings.Add("Table", "Net-informations.com");
    		DtSet = new System.Data.DataSet();
    		MyCommand.Fill(DtSet);
    		DataGridView1.DataSource = DtSet.Tables[0];
    		MyConnection.Close();
    
    	}
    

    If you still got problem, please post a demo and explain the senario about what you are going to do.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Helen Zhou Thursday, January 26, 2012 7:10 AM
    Thursday, January 26, 2012 6:24 AM
  • Thanks Helen !

    My problem was solved....

    i used  this.Hide() method instead of this.close() method in the preview form colse button.


    kavitha
    • Marked as answer by Helen Zhou Thursday, January 26, 2012 7:10 AM
    Thursday, January 26, 2012 6:34 AM

All replies

  • The error means that you are trying to access an object which was garbage collected(i.e, disposed)

    You can use the IsDisposed property to make use if the control is accesible or valid.

    -=-=-=-=-=-=-=-=-=-=

    Learn, Earn, Enjoy.....!


    Thanks, Satish Bommideni
    • Proposed as answer by Spartan_578 Wednesday, January 25, 2012 9:24 AM
    Monday, January 23, 2012 9:24 AM
  • let me know how can i use   IsDisposed  property .....

    This is my code:

     Preview objPreview = new Preview();

        public void btnPreview_Click(object sender, EventArgs e)

                           {

                                    PreviewData();        

                             }

      public void PreviewData()

       {

             if (System.IO.File.Exists(txtFilePath.Text))

                {

                    if (objPreview == null || objPreview.IsDisposed)

                    {

                        objPreview.Show();

                        string connString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtFilePath.Text);

                       string query = String.Format("select * from [{0}]", cmdExcelSheetName.SelectedItem);

                       OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connString);

                        DataTable tb = new DataTable();

                        dataAdapter.Fill(tb);

                        tb.Rows.Add();

                        tb.Rows.Add();

                        SumOrDiffer(tb);

                        objPreview.dataGridView1.DataSource = tb;

                        }

                else

                {

                   lblExcelFilePreview.Text = " Please Select Excel File before Preview";

                }

              }

             }


    kavitha
    Monday, January 23, 2012 9:55 AM
  • let me know how can i use   IsDisposed  property .....

    IsDisposed is a boolean property which shows whether a control is still accessible or not.

    When a control is said to be disposed,it means that it is no more accessible,so the IsDisposed property holds 'True'.

    So in your code replace

    if (objPreview == null || objPreview.IsDisposed) with

    if (objPreview != null && !objPreview.IsDisposed)


    Thanks, Satish Bommideni
    Monday, January 23, 2012 10:07 AM
  • Even the control is still accessible .....i have to use the control...is this possible 
    kavitha
    Monday, January 23, 2012 10:13 AM
  • Hi Kavitha,

    Was the Preview class a form which has a datagridview on it? Based on my understanding, you want to select a Excel(through what?) to preview, and once it been selected, and click a button named btnPreview on the main form, the form Preview(which has a DataGridview control on it) will open and the data in the seleced Excel file will be populated to the DataGridView. If I'm misunderstand your question, please do elaborate more clearly about your senario.

    I can't see any problems with the bit of code you've posted but somewhere along the line ibjPreview is being disposed of. 

    To import data from an Excel sheet to a DataGridView control, here is the code:

             private void Button1_Click(System.Object sender, System.EventArgs e)
    	{
    		System.Data.OleDb.OleDbConnection MyConnection = null;
    		System.Data.DataSet DtSet = null;
    		System.Data.OleDb.OleDbDataAdapter MyCommand = null;
    		MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\vb.net-informations.xls';Extended Properties=Excel 8.0;");
    		MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
    		MyCommand.TableMappings.Add("Table", "Net-informations.com");
    		DtSet = new System.Data.DataSet();
    		MyCommand.Fill(DtSet);
    		DataGridView1.DataSource = DtSet.Tables[0];
    		MyConnection.Close();
    
    	}
    

    If you still got problem, please post a demo and explain the senario about what you are going to do.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Helen Zhou Thursday, January 26, 2012 7:10 AM
    Thursday, January 26, 2012 6:24 AM
  • Thanks Helen !

    My problem was solved....

    i used  this.Hide() method instead of this.close() method in the preview form colse button.


    kavitha
    • Marked as answer by Helen Zhou Thursday, January 26, 2012 7:10 AM
    Thursday, January 26, 2012 6:34 AM
  • Well, at last... It's really great to hear that you solved the issue. Well done!
    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 26, 2012 7:10 AM