Re: Why "Specified cast is not valid" error?
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 11/23/04
- Next message: Cecilio: "Send Mail in Exchange 2003 with c#"
- Previous message: Dennis Myrén: "Re: Double.Parse - internalization problem"
- In reply to: Jack MacRank: "Re: Why "Specified cast is not valid" error?"
- Next in thread: Kurt: "Re: Why "Specified cast is not valid" error?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 23 Nov 2004 08:40:21 -0000
Jack MacRank <jack@macrank.com> wrote:
> I set a breakpoint and looked at the GetType() of "retval" after the query
> executed. I was surprised to see it was of Int64 type. I didn't know
> C#/.NET automatically converted an object variable to another type...I
> thought it would just be a generic object type and then I could cast it to
> whatever I wanted it to.
>
> I'm still a little confused why I can't cast an Int64 to String but I will
> take your advice and use .ToString() and Convert.ToInt32() when I want a
> string or integer.
>
> Thanks to everyone who responded to my question!
I *strongly* suggest you abandon database access for the moment and
read a good book on the basics of .NET, in particular the type system.
Then read up on what casts do in C#.
I know it may sound like a backward step, but getting a good handle on
the basics is vital for writing decent code. For instance, you don't
always want to call .ToString or Convert.ToInt32() - it depends
entirely on the circumstances.
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Cecilio: "Send Mail in Exchange 2003 with c#"
- Previous message: Dennis Myrén: "Re: Double.Parse - internalization problem"
- In reply to: Jack MacRank: "Re: Why "Specified cast is not valid" error?"
- Next in thread: Kurt: "Re: Why "Specified cast is not valid" error?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|