Re: Problems with (Cast)
- From: david.dawkins@xxxxxxxxx
- Date: 21 May 2005 16:25:56 -0700
Daniel Groh wrote:
> I'm facing this error:
> Error Message:Specified cast is not valid.
>
> this line:
> char chrTipoPessoa = (char)sqlCmd.ExecuteScalar();
>
> It should returns the letter "F" or "J"!
> but with (string) it works normal...I already checked and there's no
space!
>
> Why I'm getting this error when I try to cast as char ?
If it's this method:
[C#]
public virtual object ExecuteScalar();
then you're not being returned an object that casts to a char
value. I wouldn't expect a single-character string to cast to
a char, although I didn't check.
Try something like this, and then use the debugger to check the
type of obj if it still doesn't work:
object obj = sqlCmd.ExecuteScalar();
if (obj is char) {
return (char) obj;
}
if (obj is string) {
string s = obj as string;
return s.Length == 1 ? s[0] : 0; // yeuch, but you get the idea
}
throw new Exception( "Unexpected return from SQL command: " +
obj.ToString() );
etc etc
I hope this helps
Dave D
.
- Follow-Ups:
- Re: Problems with (Cast)
- From: Daniel Groh
- Re: Problems with (Cast)
- References:
- Problems with (Cast)
- From: Daniel Groh
- Problems with (Cast)
- Prev by Date: Re: Developing frameworks: Requirements?
- Next by Date: DataTable.PrimaryKey throws exception on an empty table
- Previous by thread: Problems with (Cast)
- Next by thread: Re: Problems with (Cast)
- Index(es):
Relevant Pages
|