I have an error error Operator '>' is not defined for types 'User' and 'Integer' and Value of type 'User' cannot be converted to 'Integer'.
is there something wrong in my code?
Thanks
Dim userid = uService.GetUserById(CInt(lblid.Text))
If userid > 0 Then 'this line error
End If
Public Function GetUserById(ByVal UserNo As Integer) As User
Dim sql = $"SELECT * FROM Users WHERE id = {UserNo}"
Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
Return _conn.Query(Of User)(sql).FirstOrDefault()
End Using
End Function
GetUserById
returns a user, if this user exists, and Nothing
otherwise (because of .FirstOrDefault()
).
Instead of checking the result for a user id > 0, check the result for Nothing
Dim user = uService.GetUserById(CInt(lblid.Text))
If user IsNot Nothing Then
' Do something with user
End If
A safer way to do this is to check whether the user input is a valid integer:
Dim userId As Integer
If Integer.TryParse(lblid.Text, userId) Then
Dim user = uService.GetUserById(userId)
If user IsNot Nothing Then
' Do something with user
End If
End If