|
-
Nov 20th, 2003, 07:45 PM
#1
Thread Starter
Addicted Member
InvalidCastException (RESOLVED)
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DBNull' to type 'String' is not valid.
Is there an easy way to handle this exception? I am trying to use isNull right now, but it seems bulky.
Last edited by GSIV; Nov 20th, 2003 at 09:22 PM.
-
Nov 20th, 2003, 08:27 PM
#2
Hyperactive Member
Try using something like this:
Code:
If Not myDataReader.Item("Person_Name") Is DBNull.Value Then
txtPersonName.Text = myDataReader.Item("Person_Name")
Else : txtPersonName.Text = ""
End If
-
Nov 20th, 2003, 09:16 PM
#3
Try using GetString and see if that works. I haven't tried it but it should convert DBNulls to String.Empty for you.
VB Code:
txtPersonName.Text=myDataReader.GetString("Person_Name")
-
Nov 20th, 2003, 09:22 PM
#4
Thread Starter
Addicted Member
Thanks guys!
-
Nov 21st, 2003, 11:30 AM
#5
Hyperactive Member
No, it does not convert DBNull's to String.Empty. If you try to set a string equal to DBNull it will through an exception, and that is the problem he was having.
I wouldn't want it to convert it to String.Empty. There are some cases where I would want to know if it were a Null value being returned from the database.
-
Nov 21st, 2003, 01:30 PM
#6
Originally posted by crpietschmann
No, it does not convert DBNull's to String.Empty. If you try to set a string equal to DBNull it will through an exception, and that is the problem he was having.
I wouldn't want it to convert it to String.Empty. There are some cases where I would want to know if it were a Null value being returned from the database.
Bummer it doesn't convert it for you.
Although what are you talking about you 'wouldn't want it to convert it to String.Empty'? That is exactly what you did in your example: Else : txtPersonName.Text = ""
I agree there are times you may want to know if it returned DBNull or not but it is common to substitute DBNull with String.Empty while in memory. If you want to maintain Null in the DB you can convert it back when you write to the DB.
-
Nov 22nd, 2003, 08:46 AM
#7
Hyperactive Member
Originally posted by Edneeis
Although what are you talking about you 'wouldn't want it to convert it to String.Empty'? That is exactly what you did in your example: Else : txtPersonName.Text = ""
In that specific case, Yes, I wanted it to be converted.
After some thought, I do suppose that it would be nice for it to convert it to String.Empty when you are setting as string variable equal to the value that might be DBNull. It would eliminate the need for the code that I posted above.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|