I have the following function that obtains data from a DB in Access. I throw the commented error in the header.
I made a stop point, to know more or less in which place the error was occurring and I managed to find it on the next line.
p4 = Convert.ToInt32(validacampo(zp_.Rows(cont)("pax_evaden_p4")))
What I understand is that the field in the database can be null, so create the "validacampo" function, so that it will return a value of zero "0" if the value is null, or the value in case not to be null.
Private Function validacampo(v As Object) As Integer
If v.Equals(DBNull.Value) Then
Return 0
Else
Return v
End If
End Function
The following is the complete function that works the data from the query, I hope you can help me.
Private Function ValTotales(ppu As String, sentido As String, conexion As OleDbConnection) As Object
Dim zp_ As New DataTable
Dim cont As Integer = 0
Dim evasores, p1, p2, p3, p4, val, totalusuarios, zp As Integer
Dim array(4) As Integer
Dim query_string As String = "SELECT * FROM Evasiones WHERE ppu='" + ppu + "' AND sentido_serv='" + sentido + "'"
Dim cmm As New OleDbCommand(query_string, conexion)
Dim dr As New OleDbDataAdapter(cmm)
dr.Fill(zp_)
While zp_.Rows.Count > cont
val += Convert.ToInt32(zp_.Rows(cont)("pax_validan_p1"))
p1 += Convert.ToInt32(zp_.Rows(cont)("pax_evaden_p1"))
p2 += Convert.ToInt32(zp_.Rows(cont)("pax_evaden_p2"))
p3 += Convert.ToInt32(zp_.Rows(cont)("pax_evaden_p3"))
p4 = Convert.ToInt32(validacampo(zp_.Rows(cont)("pax_evaden_p4")))
Dim zp_str As String = Convert.ToString(zp_.Rows(cont)("zona_paga"))
Dim new_str As String = Mid(zp_str, 1, 6)
If zp_str <> "" And Mid(new_str, 1, 3) = "PAX" Then
Dim a As Object = Split(new_str, " ")
If Mid(a(1), 2, 1) = "," Then
zp += Convert.ToInt32(Mid(a(1), 1, 1))
Else
zp += Convert.ToInt32(a(1))
End If
End If
cont += 1
End While
evasores = p1 + p2 + p3 + p4
totalusuarios = val + p1 + p2 + p3 + p4
array(0) = cont
array(1) = zp
array(2) = val
array(3) = evasores
array(4) = totalusuarios
Return array
End Function