Unable to access a discarded object

Asked

Viewed 2,509 times

0

I am instantiating a form from a double click on a Datagridview from another form, where through this return the information from that line to the form.

It is working properly but when I close this form and double click again on the same line or another to open the form again I get the following error:

Unable to access a discarded object.

The code where the error occurs is this:

 private void dgvDados_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
        if (dgvDados.SelectedRows.Count > 0)
        {
            this.exibirdados.txtcnpj.Text = dgvDados.SelectedRows[0].Cells[0].Value.ToString();
            this.exibirdados.dtpcadastro.Text = dgvDados.CurrentRow.Cells[1].Value.ToString();
            this.exibirdados.txtrazao.Text = dgvDados.CurrentRow.Cells[2].Value.ToString();
            this.exibirdados.cmboperadora.Text = dgvDados.CurrentRow.Cells[3].Value.ToString();
            this.exibirdados.txtlinhas.Text = dgvDados.CurrentRow.Cells[4].Value.ToString();
            this.exibirdados.cmbClassificacao.Text = dgvDados.CurrentRow.Cells[5].Value.ToString();
            this.exibirdados.dtpvigencia.Text = dgvDados.CurrentRow.Cells[6].Value.ToString();
            this.exibirdados.txtcontrato.Text = dgvDados.CurrentRow.Cells[7].Value.ToString();
            this.exibirdados.cmbFidelidade.Text = dgvDados.CurrentRow.Cells[8].Value.ToString();
            this.exibirdados.txtvalorgasto.Text = dgvDados.CurrentRow.Cells[9].Value.ToString();
            this.exibirdados.txtfixoempresa.Text = dgvDados.CurrentRow.Cells[10].Value.ToString();
            this.exibirdados.txtgestor.Text = dgvDados.CurrentRow.Cells[11].Value.ToString();
            this.exibirdados.txtcelular.Text = dgvDados.CurrentRow.Cells[12].Value.ToString();
            this.exibirdados.txtfixogestor.Text = dgvDados.CurrentRow.Cells[13].Value.ToString();
            this.exibirdados.txtemail.Text = dgvDados.CurrentRow.Cells[14].Value.ToString();
            this.exibirdados.txtobs.Text = dgvDados.CurrentRow.Cells[15].Value.ToString();
        }

        this.exibirdados.ShowDialog(); //aqui acontece o erro
    }

Form instance code:

public partial class TelaInicio : MetroFramework.Forms.MetroForm
{

    internal ExibirDados exibirdados = null;

    public TelaInicio(ExibirDados exibirdados)
    {
        InitializeComponent();
        this.exibirdados = exibirdados;
    }
  • 1

    Use the new on the line internal ExibirDados exibirdados = new ExibirDados();

  • @W.Faustino proceeds with the same error

  • 1

    Tries to create the object ExibirDados within dgvDados_CellContentDoubleClick without using the this.. I know you’re not gonna keep the GC clean but... it might work well.

  • @W.Faustino if I do this it will open and close without error, but it will not recover the information for the form, since I am receiving them from the instance exibirdados

  • 2

    Paste all the code there! It’s easier for us to help you.

  • I managed to sort out, in part what you said was right, thank you

Show 1 more comment

2 answers

1


The detail is in the this since I am assigning the retrieval of the information to the instance exibirdados I must instantiate a new object from it, I was trying to instantiate from a new object.

So the form opened without errors but brought me the empty form, the correct form would be this:

private void dgvDados_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        this.exibirdados = new ExibirDados();

        if (dgvDados.SelectedRows.Count > 0)
        {
            this.exibirdados.txtcnpj.Text = dgvDados.SelectedRows[0].Cells[0].Value.ToString();
            this.exibirdados.dtpcadastro.Text = dgvDados.CurrentRow.Cells[1].Value.ToString();
            this.exibirdados.txtrazao.Text = dgvDados.CurrentRow.Cells[2].Value.ToString();
            this.exibirdados.cmboperadora.Text = dgvDados.CurrentRow.Cells[3].Value.ToString();
            this.exibirdados.txtlinhas.Text = dgvDados.CurrentRow.Cells[4].Value.ToString();
            this.exibirdados.cmbClassificacao.Text = dgvDados.CurrentRow.Cells[5].Value.ToString();
            this.exibirdados.dtpvigencia.Text = dgvDados.CurrentRow.Cells[6].Value.ToString();
            this.exibirdados.txtcontrato.Text = dgvDados.CurrentRow.Cells[7].Value.ToString();
            this.exibirdados.cmbFidelidade.Text = dgvDados.CurrentRow.Cells[8].Value.ToString();
            this.exibirdados.txtvalorgasto.Text = dgvDados.CurrentRow.Cells[9].Value.ToString();
            this.exibirdados.txtfixoempresa.Text = dgvDados.CurrentRow.Cells[10].Value.ToString();
            this.exibirdados.txtgestor.Text = dgvDados.CurrentRow.Cells[11].Value.ToString();
            this.exibirdados.txtcelular.Text = dgvDados.CurrentRow.Cells[12].Value.ToString();
            this.exibirdados.txtfixogestor.Text = dgvDados.CurrentRow.Cells[13].Value.ToString();
            this.exibirdados.txtemail.Text = dgvDados.CurrentRow.Cells[14].Value.ToString();
            this.exibirdados.txtobs.Text = dgvDados.CurrentRow.Cells[15].Value.ToString();
        }

        this.exibirdados.ShowDialog();
    }

0

I’m not sure okay. but I think you are deleting the form that opens with the data after the first display and trying to display an object that has already been deleted in the second display attempt.

attempt at a solution: instantiate a new display object always when executing the click event and assign the new data after the instance of the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.