|
-
Jul 19th, 2009, 09:46 PM
#1
Thread Starter
Lively Member
Conversion from type 'DBNull' to type 'String' is not valid
I met that error when i use this code :
Code:
Dim K As String = sqlcommand.ExecuteScalar()
If items in column is empty it will show that error,
How can i overcome it without use trycatch or ...
-
Jul 19th, 2009, 09:48 PM
#2
Fanatic Member
Re: Conversion from type 'DBNull' to type 'String' is not valid
 Originally Posted by taybacuniversity
I met that error when i use this code :
Code:
Dim K As String = sqlcommand.ExecuteScalar()
If items in column is empty it will show that error,
How can i overcome it without use trycatch or ...
If you want to overcome it you have to call string method to convert to string type.
Code:
Dim K As String = sqlcommand.ExecuteScalar.tostring
-
Jul 19th, 2009, 10:59 PM
#3
Re: Conversion from type 'DBNull' to type 'String' is not valid
It depends exactly what you're trying to achieve, which you have conveniently failed to tell us. The previous suggestion will return an empty string so, if that's what you want in that situation, that's what you should do. Alternatively you can assign the result to an Object variable and then check whether it is DBNull.Value first, before casting as type String.
If your original code compiled then you must have Option Strict Off, which I suggest you change for this and every other project.
-
Jul 20th, 2009, 12:37 AM
#4
Thread Starter
Lively Member
Re: Conversion from type 'DBNull' to type 'String' is not valid
Thanks for all.
I used tostring method and it work.
Did that method convert to string ?
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
|