简体   繁体   中英

If datagridview cell isnot null then ? else?

While inserting into SQL I have nullable column Machine ID. If the field has something type into it I want it to check if the value exist. This worked fine. But then since its not a required table entry I want it to ignore the field if empty and just insert null. Now I've tried this a few different ways and it either continues to verify the field even if empty, or it add everything without verifying. Here was the latest attempt:

        If IsDBNull(DataGridView3.Rows(i).Cells("Machine ID").Value) = False Then

            STSQL = "select machine_id from mpcs.machine where machine_id = " & "'" & DataGridView3.Rows(i).Cells("Machine ID").Value & "'"
            rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)
            If Not rsMPCS.HasRows Then
                MessageBox.Show("Not a valid Machine ID")
                Return
            End If
            rsMPCS.Close()
        ElseIf IsDBNull(DataGridView3.Rows(i).Cells("Machine ID").Value) = True Then
            DataGridView3.Rows(i).Cells("Machine ID").Value = vbNull
        End If

Ok I figured it out this way works.

        If MachID = Nothing Then
            MachID = vbNull
        Else
            STSQL = "select machine_id from mpcs.machine where machine_id = " & "'" & DataGridView3.Rows(i).Cells("Machine ID").Value & "'"
            rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)
            If Not rsMPCS.HasRows Then
                MessageBox.Show("Not a valid Machine ID")
                Return
            End If
            rsMPCS.Close()
        End If

If I'm not mistaken it should be

DataGridView3.Rows(i).Cells("Machine ID").Value = nothing

And your your if clause is wrong. The correct syntax would be If Convert.IsDBNull(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM