views:

134

answers:

3

I'm using values from a row on a datagridview, to build an image path to pass to a picturebox.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    DataGridViewRow currentRow = new DataGridViewRow();
    currentRow = dataGridView1.CurrentRow;
    string valueJob = currentRow.Cells[2].Value.ToString();
    string valueBatch = currentRow.Cells[3].Value.ToString();
    string valueImmagine = currentRow.Cells[4].Value.ToString();
    string result = Octopus2.Properties.Settings.Default.DataPath + "\\" + valueJob + "\\" + valueBatch + "\\" + valueImmagine + ".jpg";   
    pictbox.ImageLocation = result;
}

The problem is, when i do a DataSet.clear() using another control, the code returns the following error: "NullreferenceException was unhandled by user code".

Thank you in advance, any help will be appreciated.

+1  A: 

Although I cannot se DataSet.clear() in the code you have shown but if DataSet.clear() is giving this error then DataSet is null.

Vinay Pandey
A: 

As i mentioned before, the DataSet.clear() is called by another eventhandler called by a control, a reset button. I solved the problem by doing this:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.RowCount >=1)
    {
        DataGridViewRow currentRow = new DataGridViewRow();
        currentRow = dataGridView1.CurrentRow;
        string valueJob = currentRow.Cells[2].Value.ToString();
        string valueBatch = currentRow.Cells[3].Value.ToString();
        string valueImmagine = currentRow.Cells[4].Value.ToString();

        string result = Octopus2.Properties.Settings.Default.DataPath + "\\" + valueJob + "\\" + valueBatch + "\\" + valueImmagine + ".jpg";

        pictboxImpegnativa.ImageLocation = result;
    }
    else
    {
        MessageBox.Show("good work.");
    }
}

Thank you for your time and help.

Enrico Castellani
A: 

You forgot to check if currentRow.Cells[x].Value could also be null. You should add another code that looks like this:

if (currentRow.Cells[n].Value != null) { // your code here... }

Trust me, there's an inner object you need to catch for nulls not just the one from DataGridViewRow.

Benj Nunez