Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following part of code:

VB
If IsNothing(strCellValue = tabledgv.Rows(newNum).Cells(1).Value.ToString()) Then

            datetxt.Text = "N/A"
        Else
            datetxt.Text = strCellValue

        End If


As you can see I have isNothing so it should check for that correct? Anyways when I run it I still get an error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Windows.Forms.DataGridViewCell.Value.get returned Nothing.


What I have tried:

I've tried removing the IsNothing. I also tried using isDBNull as it is a null value on SQL but have fixed that with the .tostring at the end.
Posted
Updated 26-Nov-19 22:00pm
Comments
[no name] 26-Nov-19 12:35pm    
tabledgv.Rows(newNum).Cells(1): Looks like the Row/Col does not exists.
Cody O'Meara 26-Nov-19 13:58pm    
Thank you for the response. Correct. Inside the cell there is no value so I'm trying to get it to say "N/A" when there is nothing in the cell.
[no name] 26-Nov-19 14:04pm    
I think you misunderstand this. I think the problem is more that tabledgv.Rows(newNum) or then the cell does not exists. This you _can't_ check by accessing tabledgv.Rows(newNum).Cells(1).Value. You need to make sure before that the requested row and also the requested col does exists.
Cody O'Meara 26-Nov-19 14:18pm    
I probably am missing something. I have this "totalRows = tabledgv.Rows.GetRowCount(DataGridViewElementStates.Visible) - 1" for getting total rows. It does work as I have a label that displays on load to which is correct amount. I also have this checker"If newNum > totalRows Then newNum = 1 End If" to reset back to the first row which there are 2 rows with data. Happens when trying the second row. This happens before the check to see if strCellValue equals nothing.
[no name] 26-Nov-19 14:52pm    
why -1 in totalRows = tabledgv.Rows.GetRowCount(DataGridViewElementStates.Visible) - 1?

In this blog it is advised not to use IsNothing(): Patrick Steele's .NET Blog - Avoiding IsNothing()[^]
Instead you can use:
VB
If tabledgv.Rows(newNum).Cells(1).Value Is Nothing Then
...
 
Share this answer
 
v2
Comments
Cody O'Meara 26-Nov-19 13:40pm    
Thank you for the response RickZeeland. I change it. Now getting Suppression State
Error BC30020 'Is' operator does not accept operands of type 'Boolean'. Operands must be reference or nullable types.
RickZeeland 26-Nov-19 14:44pm    
Could it be a Checkbox maybe? then use:
If (tabledgv.Rows(newNum).Cells(1).Value) Then
...
You are doing it wrong!

First of all, you can't initiate variable inside IsNothing function, because the underlined part of line is interpreted by compiler like a boolean value (a result of comparison):
VB
If IsNothing(strCellValue = tabledgv.Rows(newNum).Cells(1).Value.ToString()) Then

Boolean value will never return null/nothing!

comparison of strCellValue to the tabledgv cell Value
"" = "" ' returns true
"" = "some value" ' returns false


If your DataGrdView object is bind to the datatable of SQL database, i'd change it to:
VB
If DbNull.Value.Equals(tabledgv.Rows(newNum).Cells(1).Value) Then


For further details, please see:
Comparison Operators - Visual Basic | Microsoft Docs[^]
Information.IsNothing(Object) Method (Microsoft.VisualBasic) | Microsoft Docs[^]
DBNull.Value Field (System) | Microsoft Docs[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900