Sign In| Join
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
Member
129 Points
194 Posts
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 Get Return _usrid End Get Set(ByVal value As Nullable) _usrid = value End Set End Property
End Class _________________________________________________________________
This is my next class file called MySqlCode:
1'code 2'code 3'code 4 While reader.Read() 5 SeeField.Usrid = CType(reader("USRID"), Nullable) 6 Customer.Add(SeeField) 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?
All-Star
27666 Points
4574 Posts
Jan 25, 2013 03:24 PM|LINK
You can read a nullable fields value by using the Value property :
YourNullableField.Value
Jan 25, 2013 05:19 PM|LINK
Thanks, but where do you setup the value property?
I would not know where to place that.
Contributor
5132 Points
1465 Posts
Jan 26, 2013 08:54 AM|LINK
xyz789 xyz789
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") ?? ""
xyz789
Member
129 Points
194 Posts
Unable to cast object of type 'System.String' to type 'System.Nullable'
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
Get
Return _usrid
End Get
Set(ByVal value As Nullable)
_usrid = value
End Set
End Property
End Class
_________________________________________________________________
This is my next class file called MySqlCode:
1'code
2'code
3'code
4 While reader.Read()
5 SeeField.Usrid = CType(reader("USRID"), Nullable)
6 Customer.Add(SeeField)
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?
Rion William...
All-Star
27666 Points
4574 Posts
Re: Unable to cast object of type 'System.String' to type 'System.Nullable'
Jan 25, 2013 03:24 PM|LINK
You can read a nullable fields value by using the Value property :
xyz789
Member
129 Points
194 Posts
Re: Unable to cast object of type 'System.String' to type 'System.Nullable'
Jan 25, 2013 05:19 PM|LINK
Thanks, but where do you setup the value property?
I would not know where to place that.
thaicarrot
Contributor
5132 Points
1465 Posts
Re: Unable to cast object of type 'System.String' to type 'System.Nullable'
Jan 26, 2013 08:54 AM|LINK
Private _userid As String not null then
In C# SeeField.Usrid = reader("USRID") == null ?? string.Empty;
Weera
Rion William...
All-Star
27666 Points
4574 Posts
Re: Unable to cast object of type 'System.String' to type 'System.Nullable'
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") ?? ""