Empty Field Datatable visual basic window form

0

Good afternoon a query I'm doing in window form an application that extracts me when I enter the name of a field but that field is still empty but when I run an error jumps me from this: the field is empty but in the row it goes out as 1 and enters the condition but as it is null that error comes out

My code is as follows:

 dt = New DataTable
            dt = vB_Atencion.MostrarUsuarioIncidencia(2, TxtTicket.Text, CboAsignar.Text).Tables(0)
            If dt.Rows.Count > 0 Then
                lblUsuarioAsignado.Text = dt.Rows(0)("AsignarA")

            End If
    
asked by PieroDev 11.05.2017 в 21:24
source

1 answer

2

The problem that happens to you is that you want to assign to a property String (which is lblUsuarioAsignado.Text) a value that is read as DBNull , that's why the error.

To save the rest you have two options:

The first is to convert the object into question in a String, with the .ToString method

lblUsuarioAsignado.Text = dt.Rows(0)("AsignarA").ToString

The second option is to save the fact that this object can be DBNull , and this can be done with the IsDBNull ( object ) check >

If dt.Rows.Count > 0 Then
 If IsDBNull(dt.Rows(0)("AsignarA")) = False then
   lblUsuarioAsignado.Text = dt.Rows(0)("AsignarA")
 Else
   lblUsuarioAsignado.Text = ""
 End If
End If
    
answered by 11.05.2017 / 21:28
source