0

I am currently working in vb.net windows express 2013 with an sql back end. I am trying to create an if statement that will look at a specific cell and make a decision based on if the cell has nothing in it or not. I think the return for an sql statement is null but im not 100% sure. I have tried lots of codes I dug up on the internet but none of them have worked for me so far. Here is the line:

Dim Row_Index As Integer = DGVStart.CurrentCell.RowIndex
 If DGVStart.Rows(Row_Index).Cells(1).Value Is Nothing Then

This code does work, as in the windows express is telling me the line is fine, however, it seems that I am always getting the return of nothing, even if something is in the cell.I think this problem has something to do with the way i am pulling the value. I have a datagridview checkbox column which i manually added in the design form and if i change it to cells(0), which is my manually inserted column, it picks it up every time. I need to know how to tell my program to differentiate between is null and is not null.

UPDATE: I am now running into new issues after changing to searching for dbnull. here is my code.

   Private Sub DGVStart_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVStart.CellContentClick

    'dim start time
    Dim start As String = TxtStart.Text

    'fail safe so headers cant be clicked
    If e.RowIndex = -1 Then
        Exit Sub
    End If

    'dim row index and check, check is for the parts shear columns
    Dim Row_Index As Integer = DGVStart.CurrentCell.RowIndex
    'Dim check As String = DGVStart.Rows(Row_Index).Cells(2).Value

    'if checkbox is clicked
    If e.ColumnIndex = 0 Then

        '---------------------------------------------Normal parts---------------------------------------------------------------

        'If DGVStart.Rows(Row_Index).Cells(1).Value Then
        If IsDBNull(DGVStart.Rows(Row_Index).Cells(3).Value) = True Then

            Dim Shear As Date = DGVStart.Rows(Row_Index).Cells(5).Value
            Dim Machine As Int16 = DGVStart.Rows(Row_Index).Cells(4).Value
            Dim ShearNumber As String = DGVStart.Rows(Row_Index).Cells(1).Value
            Dim Parts As String = DGVStart.Rows(Row_Index).Cells(3).Value

            'Pull Values for sql statement and other operations


            Try

                'set up for checkbox event to trigger
                If e.ColumnIndex = 0 Then

                    'safety messagebox
                    Dim result As Integer = MsgBox("Are you sure Shear Number " & ShearNumber & " is being started?", MessageBoxButtons.YesNo)

                    'if messagebox is no
                    If result = DialogResult.No Then
                        Exit Sub

                        'if messagebox is yes
                    ElseIf result = DialogResult.Yes Then

                        'sql update
                        Using conn1 As New SqlConnection(connstring)
                            conn1.Open()
                            Using comm1 As New SqlCommand("UPDATE table1 SET " & start & "  = getdate() Where col = value AND col = value", conn1)
                                With comm1.Parameters
                                    .AddWithValue("@Shear", Shear)
                                    .AddWithValue("@Machine", Machine)
                                End With
                                comm1.ExecuteNonQuery()
                            End Using
                            conn1.Close()
                        End Using
                    End If
                End If

            Catch ex As Exception
                MsgBox(ex.ToString)
                MsgBox("Error checking off start date for this machine, please contact the manufacturing enginnering department.")
            End Try

            Call refresh()
            Call refresh2()

            '--------------------------------------------------------Parts special--------------------------------------------------
        Else
            MsgBox("Parts")

        End If
    End If
End Sub

***Please note my sql statements have been modified to protect the company I am working for but I am sure the sql code works.

NEW PROBLEM: I am receiving a null error message because if i uncomment this line:

If DGVStart.Rows(Row_Index).Cells(1).Value Then

Then I am trying to see if a cell is null but that causes an error. I am trying to use my if statement to either run the normal parts, or special parts of my code.

Thanks,

4

0 に答える 0