0

What I am trying to do is : FIRST: Check if Cell Value Exist if TRUE continue. SECOND : If False MsgBox Invalid Ticket.

Private Sub changefound()

    Dim findtxt As String = txt_Find.Text
    Try
        If DataGridView2.Rows.Count > 0 Then
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                Dim CellChange As String = DataGridView2.Rows(i).Cells("CODE").Value.ToString 'This is line 363

                If CellChange.Contains(findtxt) = True Then
                    If Not IsDBNull(DataGridView2.Rows(i).Cells("STATUS").Value) _
    AndAlso DataGridView2.Rows(i).Cells("STATUS").Value = "IN" Then
                        MsgBox("Ticket Used")
                        Exit Sub
                    Else
                        With DataGridView2
                            .Rows(i).Cells("STATUS").Value = "IN"
                            Exit Sub
                        End With
                    End If
                End If
            Next
        End If
    Catch e As Exception
        MessageBox.Show(e.ToString())
    End Try

    '''''''''''''''''''If Flase Only Works Here''''''''''''''''''''''

    Try
        If DataGridView2.Rows.Count > 0 Then
            For i As Integer = 0 To DataGridView2.Rows.Count - 1

                Dim CellChange As String = DataGridView2.Rows(i).Cells("CODE").Value.ToString

                If CellChange.Contains(findtxt) = False Then
         MsgBox("InValid Ticket")
                    Exit Sub
                End If
            Next
        End If
    Catch e As Exception
        MessageBox.Show(e.ToString())
    End Try
End Sub

the problem is i keep getting the error MSGBOX enter image description here

Mathemats
  • 1,153
  • 4
  • 22
  • 32
db35m
  • 103
  • 1
  • 1
  • 13
  • Well, you have `Catch e As Exception MessageBox.Show(e.ToString())`, so a message box with the error is kind of expected. Did you expect anything else? Please specify. Also please take care to format your code, so that it looks nice. Take out everything we don't need to see, that is, leave only code that pertains to your problem. – Neolisk Mar 29 '15 at 01:29
  • get rid of the Try/Catch and fix the actual error - an NRE isnt something that can be ignored or just logged. Whatever is nothing can be tested for with `If var Is Nothing Then...` – Ňɏssa Pøngjǣrdenlarp Mar 29 '15 at 01:30
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 29 '15 at 01:35
  • @Plutonix if i switch the try exception to msgbox("Not Found") it works but i want to be able to execute the Try...and be able to have both If CellChange.Contains(findtxt) = True Then do something elseif If CellChange.Contains(findtxt) = False Then do other thing End If – db35m Mar 29 '15 at 01:35
  • 1
    see the link - if users can add rows your loop is one too long – Ňɏssa Pøngjǣrdenlarp Mar 29 '15 at 01:38

0 Answers0