0
votes

GOAL: I want to query all rows on a datagridview and check if two columns on the same row are equal to a specific value. I am getting an "object reference equal to null" error because I am using a foreach loop. What is the best possible way to perform this task? Show example

Current code:

foreach (DataGridViewRow row in dataTABLE.Rows)
{                                                            
if ((row.Cells[0].Value.ToString().ToUpper().Equals(MyServerChecked)) & (row.Cells[1].Value.ToString().ToUpper().Equals(service.ServiceName.ToString().ToUpper())))
}

values get written to the dataTABLE when a button is clicked. inside that button, this foreach loop exists because I do not want the same row to be created multiple times. That's why I am trying to query column values and compare them

2

2 Answers

1
votes

foreach has nothing to do with null reference exception, unless you're deleting some rows while processing them. You did not provide any info on which object are you getting the error, but you cannot call row.Cells[0].Value or row.Cells[0].Value's ToString() methods if they're null. You could add additional condition to your if statement:

if(row.Cells[0].Value!=null && row.Cells[1].Value!=null
    && row.Cells[0].Value.ToString().ToUpper().Equals(MyServerChecked)
    & row.Cells[1].Value.ToString().ToUpper().Equals(service.ServiceName.ToString().ToUpper()))
1
votes

To avoid the exception first check your objects are not null before trying to call methods on them. Check for...

dataTABLE != null
Cells[0].Value != null
Cells[1].Value != null

That being said, you asked about the best possible way to achieve the task so here's some other things to think about.

You could just iterate through the data table using something like dataTable.AsEnumerable().Any( dr => ...); to see if any rows matching your criteria already exist.

Whenever you need to compare strings to other strings or objects that could be null, you need to think carefully about your requirements and what a null reference means for you.

If cell[0].value == null do you still want to go ahead and compare MyServerChecked? If cell[0].value == null and MyServerChecked == null then should they be considered equal?

Similarly, how do you want to handle empty strings? Should an empty string be considered equal to null?

Here is a simple solution to get started. If the cells are null, it won't try to compare them. This may not be the behavior you want, but like I said, have a think about what null values and empty strings means for your particular scenario and code for it accordingly.

string MyServerChecked = "Something";
string ServiceName = service.ServiceName.ToString();

if (dataTable != null)
{
    foreach (DataGridViewRow row in dataTable.Rows)
    {
        if (row.Cells[0].Value != null
            && row.Cells[1].Value != null
            && string.Equals(row.Cells[0].Value.ToString(), MyServerChecked, StringComparison.OrdinalIgnoreCase)
            && string.Equals(row.Cells[1].Value.ToString(), ServiceName, StringComparison.OrdinalIgnoreCase))
        {
            // Row already exists...
        }
    }
}

Using the overloaded string.Equals method with a StringComparison enum type means you don't have to convert to upper yourself. It also gives you some extra options for culture settings. Have a look at StringComparison Enumeration on MSDN for more info.