Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 19, 2008 03:57 PM by firstname.lastname@example.org
Mar 19, 2008 11:22 AM|LINK
Hi all, I was using VB to write the following code and got the "Specified cast is not valid. " error. Any idea to solve that? Thanks in advance.
Mar 19, 2008 12:10 PM|LINK
For the user you are trying to get have you checked that the SQL returns non NULL values and values that can be converted to the types you are converting to? I.e. run the SQL in Management Studio and check that...
Mar 19, 2008 01:22 PM|LINK
Mar 19, 2008 01:30 PM|LINK
Scott is right. You should check if the value is null or not.
Also you can use Convert.ToInt32() or .ToString() functions if the problem is only because of casting and not the null value.
Hope this will help you!
Nepster from India! [Yes]
Please click "Mark as Answer" to this post if you get any help.
Mar 19, 2008 01:42 PM|LINK
Yeah you could use an In Line IF or you could do what nepster_from_india suggests or you could even check in the SQL like so:
Run the SQL in your management studio to check to see if any of the fields are returning NULL or if they are returning values that cannot be cast as the types you want, i.e. if the column UserProfileId stores a value of 'Dave' it won't be
able to be converted into an integer.
Mar 19, 2008 02:33 PM|LINK
Hi all, I changed the code as below and still got the same error, anything wrong with my code? Thanks.
Mar 19, 2008 02:45 PM|LINK
Are you positive the columns RegistrationDate / LastChange if not null are of type DateTime? Can you run your SQL for the user you are trying to get and post the results i.e. run it from SQL management studio.
Mar 19, 2008 02:52 PM|LINK
Immediate If (IIF) will not work in this context because it ALWAYS evaluates both sides of the expression :(
Mar 19, 2008 03:06 PM|LINK
The value of LastChange field is normally null,So I used inline if statement to check it. If it's null then I used
RegistrationDate value which was not null value,else use Now() to get current time.Then it works. Thanks.
Mar 19, 2008 03:10 PM|LINK