Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 26, 2013 12:11 PM by Rion Williams
Jan 25, 2013 03:16 PM|LINK
I have the following code in a class file called MyVariables:
Public Class MyVariables
Private _userid As Nullable
Public Property Usrid As Nullable
Set(ByVal value As Nullable)
_usrid = value
This is my next class file called MySqlCode:
4 While reader.Read()
5 SeeField.Usrid = CType(reader("USRID"), Nullable)
7 End While
On line 5 I will get the error message "Unable to cast object of type 'System.String' to type 'System.Nullable'"
I don't beleive I have an object set to String, so my question is how can I read a field that some times will have Null values?
Jan 25, 2013 03:24 PM|LINK
You can read a nullable fields value by using the Value property :
Jan 25, 2013 05:19 PM|LINK
Thanks, but where do you setup the value property?
I would not know where to place that.
Jan 26, 2013 08:54 AM|LINK
Private _userid As String not null then
In C# SeeField.Usrid = reader("USRID") == null ?? string.Empty;
Jan 26, 2013 12:11 PM|LINK
Have you tried not casting it as a nullable when you are reading it?
SeeField.Usrid = reader("USRID")
You could combine the above with the null coalescing operator to handle if it was null as well :
'This will read the USRID field and set the value according. If it is null it will set it to "" '
SeeField.Usrid = reader("USRID") ?? ""