I have a for loop that iterates through a datagrid to verify all cells are formatted correctly, one of the validations is checking that the 1st column on every row (Description) is entered and at least 10 characters.
In the for loop i have the following
$descriptionLength = ($datagridview1.Rows[$i].Cells[0].Value).ToString().Length
If ($descriptionLength -le 9)
{
$validated = $false
$richtextbox1Errors.Visible = $true
$richtextbox1Errors.AppendText(" ERROR: Line $($i+1) - Description should have at least 10 characters.$nl")
}
This works if i have something entered but if the cell is blank i get the error “You cannot call a method on a null-valued expression.”
I have tried a couple different ways and can’t seem to find a way to handle a null value, the error is complaning about the line setting the variable rather than the if statement.
I did previous try without setting a variable as per this:
If ((($datagridview1.Rows[$i].Cells[0].Value).ToString()).Length -le 9)
{
$validated = $false
$richtextbox1Errors.Visible = $true
$richtextbox1Errors.AppendText(" ERROR: Line $($i+1) - Description should have at least 10 characters.$nl")
}
it’s hard to reproduce the issue without any data to play with. But I think your if statement checks the wrong property. I’d try to check if there is a value at all before you can measure the length.
if ($datagridview1.Rows[$i].Cells[0].Value)
{
"...."
}
This is actually what im trying to do, the above examples work when something exists, i.e. when i have 1 to 9 characters however, when there is nothing (null) i don’t know how to check for that.
How do i check a datagridview cell to see if its null?
I also tried
if ($datagridview1.Rows[$i].Cells[0].Value -eq $null)
{
"...."
}
That didn’t work either, still got the error “You cannot call a method on a null-valued expression.”
I’m afraid you did not get what I tried to explain.
Again: “Nothing” is NOT EQUAL to “NULL” !!! NULL is a valid value. If a variable contains the vaule “NULL” it is not empty. If your code works like this at the moment it may not work anymore if there is really an empty cell. That’s why there are some methods called .IsNullOrEmpty(). That are two different states - Null OR Empty !!
Thank you for re-iterating your point, although my script started working with the change in order of the operator, i did some further reasearch to understand your point more and hopefully i understand what you mean by $null not meaning “nothing”
So where my understanding was wrong is… (I think)
I was checking if the cell is not equal to $null, thinking $null would be “Nothing” and therefore if it is not equal to $null then it must have a something entered.
If ($datagridview1.Rows[$i].Cells[0].Value -ne $null)
{
...
}
But it could actually be equal to $null where $null is a value and therefore give unexpected results in some scenarios?
I think in my case this shouldn’t be an issue but…
Replacing the IF statement to do as you suggested, where it checks to see if there is ANY value at all first and then checking the legnth is less than 10 characters would be the safer way?
2 IF’s nested might not be the most elegant way, maybe they can be combined into one but…
If ($datagridview1.Rows[$i].Cells[0].Value)
{
if (($datagridview1.Rows[$i].Cells[0].Value.ToString()).Length -le 9)
{
...
}
} else { ... }
Hopefully this is me understanding it a little better