Object reference not set as an instance of an object. Problem with Datatables in C #

1

How can I get the record of my selected row? What I would like is that when selecting a row this selection I return the record to then get its Id and finally make an update in case of any modification.

Current code of the method to modify:

DataGridViewRow row = dataTabla.CurrentCell.OwningRow;
string value = row.Cells["Id"].Value.ToString();

I get the following error:

  

Reference to object not established as an instance of an object.

Only that error comes to me at the beginning I can not even enter the screen where the table is and I do not know why it is executed at the beginning if it should be executed when a row is selected. It is a method of type cellValueChanged .

    
asked by David 03.04.2017 в 23:32
source

3 answers

1

If you want to save the Id, use the RowEnter event. Then use the value of e.rowIndex to know the index of the selected row:

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        idDeFila = dataGridView1.Rows[e.RowIndex].Cells["Id"].Value;
    }

As an alternative, you can save saving the Id every time you select a row and check the changes when you are going to validate the row. Use the event RowValidating for it. Within the method you can do all the checks you need to decide whether to save the changes or not:

private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
        if(seCumplenCondicionesDeGuardado){
          //guardar fila o hacerlo en el evento RowValidated, que se lanzará inmediatamente después
        }
        else e.Cancel = true; //cancela el evento de validación de fila
    }
    
answered by 04.04.2017 в 15:58
1

So that you can select the row you want in DataGridView there is an event called RowCommand double click on it and send it like this:

 protected void DtgSolicitudes_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DtSet = new DataSet();

        SqlConnection con = new SqlConnection(ObtenerCadenaConexion());
        con.Open();
        DtgSolicitudes.SelectedIndex = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = DtgSolicitudes.SelectedRow;
        SqlCommand cmd = new SqlCommand("SP_BuscarSeguimiento", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("OPT", 2);
        SqlParameter p2 = new SqlParameter("periodo", "");
        SqlParameter p3 = new SqlParameter("oficina", "");
        SqlParameter p4 = new SqlParameter("rubro", row.Cells[1].Text);

        cmd.Parameters.Add(p1);
        cmd.Parameters.Add(p2);
        cmd.Parameters.Add(p3);
        cmd.Parameters.Add(p4);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        TextBox1.Text = Convert.ToString(row.Cells[2].Text);
        TextBox2.Text = Convert.ToString(row.Cells[1].Text);
        TextBox3.Text = Convert.ToString(row.Cells[3].Text);
        TextBox4.Text = Convert.ToString(row.Cells[4].Text);
        TextBox5.Text = Convert.ToString(row.Cells[5].Text);
        TextBox6.Text = Convert.ToString(row.Cells[6].Text);
        TextBox7.Text = Convert.ToString(row.Cells[7].Text);
con.close();
    }
    
answered by 03.04.2017 в 23:40
1

If the value of the cell is null, it generates that error. You must verify that the value is not null:

if (row.Cells["Id"].Value != null)

or:

if (object.ReferenceEquals(row.Cells["Id"].Value, null) == false)
    
answered by 12.04.2017 в 00:39