Android Question Get DBNULL from MSSQL

DataProtec

Member
Licensed User
Hello
I have a problem to get a DBNULL from database.
I need somthing similar to

"If Not IsDBNull(reader.Item("ID".ToString)) Then sID = reader.Item("ID".ToString)"
which is from VB Net, the query Works fine, also in V4A with joins

In V4A i have
B4X:
sSQL = "SELECT p.ID, p.UserID, p.RegisterType_ID, p.CheckIn, p.CheckOut,"
    sSQL = sSQL & " f.UserID, f.Name, f.Password"
    sSQL = sSQL & " FROM CheckInOut_Table p"

    sSQL = sSQL & " INNER JOIN UserInfo_Table f"
    sSQL = sSQL & " ON p.UserID = f.UserID"

    sSQL = sSQL & " Where f.UserID = '" & aktUserID & "'"
    sSQL = sSQL & " And f.Password = '" & sPassword & "'"
    sSQL = sSQL & " Order By CheckIn DESC"
    Dim rs As JdbcResultSet = msSQL.ExecQuery(sSQL)
  
    Do While rs.NextRow
        Log(rs.GetString2(0))
        Log(rs.GetString2(1))
        Log(rs.GetString2(2))
        Log(rs.GetString2(3))
        Log(rs.GetString2(4))  '*** sometimes need to have DBNULL, this i need to know
       '*** In this way is DBNULL = True the App exit with Error Msg
      '** java.lang.NullPointerException: Attempt to invoke virtual method 'int    
      '***java.lang.String.length()' on a null object reference
    Loop

Thanks in advanced
Uli DataProtec
 

emexes

Expert
Licensed User
What does the line
B4X:
Log(rs.GetString2(4))
log when that column/field is DBNULL? Is it actually a Null of some form? I would have thought it would just be an empty string, per:

upload_2019-9-27_16-4-50.png


although that is for jSQL rather than jdbc - perhaps jdbc is different.

If .Getstring is returning types other than String, then I'd try something like:
B4X:
Dim Anything As Object = rs.GetString2(4)
Dim AnyString As String = ""    'default value
If Anything Is String Then    'presumably Null is NOT a String
    AnyString = Anything
End If
'at this point, AnyString is a valid String, regardless of what .GetString2 gave us
and then, returning to your original question, you could have something like:
B4X:
If AnyString.Trim <> "" Then    'or If AnyString.Trim.Length > 0 Then
    sID = AnyString
Else
    'no change to sID (or use some other backup/default value)
End If
 
Upvote 0
Top