Results 1 to 6 of 6

Thread: [RESOLVED] System.InvalidCastException was unhandled

  1. #1

    Thread Starter
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012
    Posts
    210

    Resolved [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
    Code:
    LookupTable = ""
    Name:  ERROR.jpg
Views: 2321
Size:  42.4 KB
    Last edited by ryanframes; Oct 1st, 2014 at 08:39 PM. Reason: Add Image
    Sorry for bad english.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: System.InvalidCastException was unhandled

    Hi,

    Is this a VB 6.0 code?

    KGC
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012
    Posts
    210

    Re: System.InvalidCastException was unhandled

    well, i won't post it in .net thread if my problem is vb 6
    Sorry for bad english.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    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.

  5. #5

    Thread Starter
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012
    Posts
    210

    Re: System.InvalidCastException was unhandled

    Quote Originally Posted by wes4dbt View Post
    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.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    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
  •  



Click Here to Expand Forum to Full Width