-
Oct 1st, 2014, 04:43 AM
#1
Thread Starter
Addicted Member
[RESOLVED] System.InvalidCastException was unhandled
Hello Guys,
How to fix InvalidCastException error ?
I have this code in my function :
Code:
If Not r.EOF Then
'LookupTable = IIf(IsDBNull(r.Fields(0).Value), "", r.Fields(0).Value)
LookupTable = IIf(IsDBNull(r(0).ToString), "", r(0).ToString)
Else
LookupTable = ""
End If
i already tried to use
Code:
r.Fields(0).ToString
but still not working.
Any suggestion ?
Should i add conditions for every datatype ?
*sigh ,if i have to do that it would be such a waste of codes i think..
Thank you.
Edit : the error is shown on
Last edited by ryanframes; Oct 1st, 2014 at 08:39 PM.
Reason: Add Image
Sorry for bad english.
-
Oct 1st, 2014, 05:10 AM
#2
Re: System.InvalidCastException was unhandled
Hi,
Is this a VB 6.0 code?
KGC
-
Oct 1st, 2014, 08:19 PM
#3
Thread Starter
Addicted Member
Re: System.InvalidCastException was unhandled
well, i won't post it in .net thread if my problem is vb 6
Sorry for bad english.
-
Oct 1st, 2014, 08:58 PM
#4
Re: System.InvalidCastException was unhandled
Well you are using ADODB when ADO .Net is the proper data access tools. Also, you are trying to return a value from a function like it's done in VB6.
Did you try this,
Code:
If Not r.EOF Then
'LookupTable = IIf(IsDBNull(r.Fields(0).Value), "", r.Fields(0).Value)
Return IIf(IsDBNull(r(0).ToString), "", r(0).ToString)
Else
Return ""
End If
There is no way this would work
Code:
If Not r.EOF Then
'LookupTable = IIf(IsDBNull(r.Fields(0).Value), "", r.Fields(0).Value)
LookupTable = IIf(IsDBNull(r(0).ToString), "", r(0).ToString)
Else
LookupTable = r(0).ToString
End If
If r.EOF is True then there is no "r(0)"
I could be wrong because I've never used a Function that Return a Variant in .Net
Last edited by wes4dbt; Oct 1st, 2014 at 09:10 PM.
-
Oct 1st, 2014, 09:13 PM
#5
Thread Starter
Addicted Member
Re: System.InvalidCastException was unhandled
Originally Posted by wes4dbt
Well you are using ADODB when ADO .Net is the proper data access tools. Also, you are trying to return a value from a function like it's done in VB6.
Did you try this,
Code:
If Not r.EOF Then
'LookupTable = IIf(IsDBNull(r.Fields(0).Value), "", r.Fields(0).Value)
Return = IIf(IsDBNull(r(0).ToString), "", r(0).ToString)
Else
Return = ""
End If
There is no way this would work
Code:
If Not r.EOF Then
'LookupTable = IIf(IsDBNull(r.Fields(0).Value), "", r.Fields(0).Value)
LookupTable = IIf(IsDBNull(r(0).ToString), "", r(0).ToString)
Else
LookupTable = r(0).ToString
End If
If r.EOF is True then there is no "r(0)"
I could be wrong because I've never used a Function that Return a Variant in .Net
ah yes, i'm sorry i forgot about that R.EOF.
i change it again, but it's still not working
i try to re-write my old vb6 project to .net, that's because i'm really really new in .net i use ado(and i don't familiar with ado.net), and that function is from my old vb6 project.. i just thought it should be work in .net too ..
and thank you , it works now.. i just need to change function's return as string not as variant.
Sorry for bad english.
-
Oct 1st, 2014, 09:19 PM
#6
Re: [RESOLVED] System.InvalidCastException was unhandled
After a little testing it seems that a function that returns a VariantType wants to return a value that can be converted in an Integer. Why are you trying to return a VariantType? If you have to then
Code:
If Not r.EOF Then
'LookupTable = IIf(IsDBNull(r.Fields(0).Value), "", r.Fields(0).Value)
Return IIf(IsDBNull(r(0).ToString), "", r(0).ToString)
Else
Return "0"
End If
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
|