0

I am currently working in VB.net 2013 with windows forms. I have a code that will select a string by looking at columns but if the end user click the header of the checkbox column the program tries to select something it cant. My question is, is there an if statement that pretty much says, IF column header of row (x) is clicked exit sub? Here is my code, it is a databound DGV. My failsafe is actually getting me here.

 Private Sub DataGridCombined_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridCombined.CellContentClick

    'failsafe to make sure only the ordered column is being clicked
    If e.ColumnIndex <> 6 Then
        Exit Sub
    End If
    'dim L as the Job number string
    Dim L As String = DataGridCombined.Rows(e.RowIndex).Cells(2).Value
    'Bring up a messagebox to double check that the correct Job was selected
    Select Case MsgBox("Are you sure you want to order Job Number " & L & "?", MsgBoxStyle.YesNo)
        'if message box user end returns no
        Case MsgBoxResult.No
            Exit Sub
            'if message box user returns yes
        Case MsgBoxResult.Yes
            Try
                'sql code to update the FCOrdered table to form channel complete
                Using conn1 As New SqlConnection(connstring)
                    conn1.Open()
                    Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET Ordered = 1 WHERE JobNumber = '" & L & "'", conn1)
                        comm1.ExecuteNonQuery()
                        conn1.Close()
                    End Using
                End Using
                'catch statement for try statement
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
    End Select
Cheddar
  • 530
  • 4
  • 30
  • Not your answer but [take a look at this](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). What do you think will happen if you type in the third column `x'; DELETE * FROM tblFCOrdered; --` (Please do not try, just imagine) – Steve Sep 12 '14 at 16:57
  • For your answer... what is the value of e.RowIndex when you click the header? – Steve Sep 12 '14 at 16:58
  • Its trying to grab data from another column header. The actual error is, an unhandled exception of type 'system.argument.outofrangeexception' occured in mscorlib.dll. – Cheddar Sep 12 '14 at 17:09
  • As far as the third column thing are you talking about an sql injection statement? I have the column locked for that case. – Cheddar Sep 12 '14 at 17:10
  • All right then, but your e.RowIndex is -1 so you get the OutOfRange. Just check for it as you do for the e.ColumnIndex at the beginning of your code – Steve Sep 12 '14 at 17:17
  • like this...IF e.columnindex = -1 THEN exit sub? – Cheddar Sep 12 '14 at 17:19

1 Answers1

0

Just add a check for e.RowIndex = -1 when you receive the event

Private Sub DataGridCombined_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridCombined.CellContentClick

    'failsafe to make sure only the ordered column is being clicked'
    If e.ColumnIndex <> 6 Then
        Exit Sub
    End If

    ' Check if the user clicks the header. Exit in that case.'
    If e.RowIndex = -1 Then
        Exit Sub
    End If

    .....

Keep in mind that CellContentClick will be fired only when you click the Cell Content (exactly the text). If you just click an empty space of the cell you don't receive this event. Probably you want to use the CellClick event instead

Steve
  • 213,761
  • 22
  • 232
  • 286