This is a migrated thread and some comments may be shown as answers.

Error in trying to get value from a cell with no value

3 Answers 91 Views
GridView
This is a migrated thread and some comments may be shown as answers.
tcl4p
Top achievements
Rank 1
tcl4p asked on 15 Feb 2011, 11:18 PM
I'm getting an error when I try to access a grid cell value, which has no value. I tried using the trycast function, but that didn't work.  Here is my code below.   When the code runs and there's a value the message box show without problem, but if there's no value in the cell I get an error (see attached file).   The grid is loaded from the a sub call (see below)  Need to know how to handle this situation.

Thanks,
Tom

  Private Sub grdIssued_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdIssued.Click
        Dim i As Integer

        If grdIssued.SelectedRows.Count > 0 Then
            '   Dim ReturnDate As String = TryCast(Me.grdIssued.Rows(Me.grdIssued.CurrentRow.Index).Cells(5).Value, String)
            MsgBox(Me.grdIssued.Rows(Me.grdIssued.CurrentRow.Index).Cells(5).Value)
        End If
        i = grdIssued.CurrentRow.Index
        clsPPE.PPEID = grdIssued.Rows(i).Cells(0).Value
    End Sub

  Private Sub LoadPPEIssuedGrid()
        Dim da As SqlDataAdapter
        Dim dt As New DataTable

        clsPPE.SelectedMemberID = Me.cboMember.SelectedValue
        da = clsPPE.GetPPEIssuedByMemberID
        da.Fill(dt)
        Me.grdIssued.DataSource = dt
        Me.grdIssued.Columns(0).IsVisible = False
        Me.grdIssued.Columns(1).Width = 100
        Me.grdIssued.Columns(2).Width = 100
        Me.grdIssued.Columns(3).Width = 100
        Me.grdIssued.Columns(4).Width = 90
        Me.grdIssued.Columns(4).FormatString = "{0: M/d/yyyy}"
        Me.grdIssued.Columns(5).Width = 90
        Me.grdIssued.Columns(5).FormatString = "{0: M/d/yyyy}"
        Me.grdIssued.CurrentRow = Nothing

    End Sub

3 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 15 Feb 2011, 11:33 PM
Hello,

The value for this cell is null, so you either need to check for a null value before messaging out the result. If it's null, you could message an empty string. Also, just a pointer, but it's better practice in my opinion to use column names rather than ordinal position in case the user re-orders the columns.
Regards,
Richard
0
Accepted
tcl4p
Top achievements
Rank 1
answered on 16 Feb 2011, 03:22 PM
Thanks for the response.  Although I knew the value was probably NULL, I was confused because vb.net does not have a IsNull function.  I had tried the trycast function and checking for NOTHING, but that didn't work as well as the string.IsNullOrEmpty function.  In the end I found the IsDBNull function to work.  For those who might come across this post looking for an answer here's the code that worked.
 If IsDBNull(Me.grdIssued.Rows(Me.grdIssued.CurrentRow.Index).Cells(5).Value) Then
                clsPPE.PPEID = grdIssued.Rows(Me.grdIssued.CurrentRow.Index).Cells(0).Value
End If

Thanks
Tom
0
Richard Slade
Top achievements
Rank 2
answered on 16 Feb 2011, 03:27 PM
Glad that you have found a solution. Yes, this will be a DbNull if getting the value from a DB. If the answer was helpful, please mark as answer, but if there's anything else you need, please let me know
thanks
Richard
Tags
GridView
Asked by
tcl4p
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
tcl4p
Top achievements
Rank 1
Share this question
or