Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to fix?

This is my code:
VB
Dim x As Integer = 0
        While x <> DGVtry.RowCount
            If dgvtry.Rows(0).Cells(1).Value = "1" Then
                LBLq1.Text = dgvtry.Rows(x).Cells(0).Value
            ElseIf dgvtry.Rows(0).Cells(1).Value = "2" Then
                LBLq2.Text = dgvtry.Rows(0).Cells(0).Value
            End If
            x = x + 1
        End While


This is my table:
VB
id    counter
101      1
104      2
105      3
107      4


Thanks in advance!
Posted

Well, the error message is pretty clear and you have to correctly handle null values (depending on your requirements you may either generate an error message or simply skip them).
 
Share this answer
 
you can do as below
VB
If NOT IsDbNull(dgvtry.Rows(0).Cells(1)) AndAlso dgvtry.Rows(0).Cells(1) = "1" Then
 
Share this answer
 
VB
 While x <> DGVtry.RowCount
    If Not IsDBNull(dgvtry.Rows(0).Cells(1)) Then

        If dgvtry.Rows(0).Cells(1).Value = "1" Then
            LBLq1.Text = dgvtry.Rows(x).Cells(0).Value
        ElseIf dgvtry.Rows(0).Cells(1).Value = "2" Then
            LBLq2.Text = dgvtry.Rows(0).Cells(0).Value
        End If

    End If
    x = x + 1
End While
 
Share this answer
 
v2

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

  Print Answers RSS


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