0

I started getting this error lately and i'm not sure why. Nothing new has changed and I could really use some help

        If e.Value = "Departure" Then
            dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightPink
            dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightPink
            dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightPink

            'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightPink
            'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor

        Else
            dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightGreen
            dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightGreen
            dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightGreen

            'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightGreen
            'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor

        End If
    End If
End Sub
4
  • You shouldn't post code in image... Commented May 17, 2018 at 14:23
  • 1
    Post your code directly, not as image that we need to navigate away to see. When you post it directly, make sure to format it properly. Commented May 17, 2018 at 14:26
  • Regardless, the issue is that you are not accounting for the fact that you might get NULLs from your database. If it wasn't happening before then there probably weren't any NULLs in your database before. Have you bothered to debug the code to see what's happening when the exception is thrown? Commented May 17, 2018 at 14:28
  • yes when i start to debug I get this for an error. System.InvalidCastException: 'Operator '=' is not defined for type 'DBNull' and string "Departure".' Commented May 17, 2018 at 14:42

1 Answer 1

2

You e.Value seems to be NULL so you need to improve the if:

If CStr("" & e.Value) = "Departure" Then
    dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightPink
    dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightPink
    dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightPink

    'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightPink
    'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor
Else
    dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightGreen
    dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightGreen
    dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightGreen

    'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightGreen
    'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor
End If

The CStr("" & e.Value) converts your e.Value to a string value:

CStr("" & DBNull.Value)  ' ""
CStr("" & Nothing)       ' ""
CStr("" & "Hello World") ' "Hello World"
Sign up to request clarification or add additional context in comments.

1 Comment

This answer will probably fix your issue, but it's worth noting that if this is a new issue that just suddenly started for you, it could be a symptom of another problem, indicating that your program is allowing nulls into a table that previous didn't allow it. So don't ignore that, if that's the case!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.