Problems with (Cast)

  • Thread starter Thread starter Daniel Groh
  • Start date Start date
D

Daniel Groh

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 ?
 
Daniel said:
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
 
it works with int too (of course if is a number)...why not as a char ? i'll
try to do as you wrote!

Daniel said:
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
 
Daniel Groh said:
it works with int too (of course if is a number)...why not as a char ?

It depends on what type the SQL is returning - what SQL are you
actually executing? My guess is that you're executing some SQL which
will return a string. Just because the string consists of a single
character doesn't change to to *being* a char - it's still a string,
and you can't cast from string to char.
 
My field in SQL is a Char(1) =/

Jon Skeet said:
It depends on what type the SQL is returning - what SQL are you
actually executing? My guess is that you're executing some SQL which
will return a string. Just because the string consists of a single
character doesn't change to to *being* a char - it's still a string,
and you can't cast from string to char.
 
Daniel Groh said:
My field in SQL is a Char(1) =/

That's a string of length 1, not a char.

Have a look in SqlDbType - there's actually nothing which corresponds
directly to a char. The closest in some ways is SmallInt, but it's
probably easiest to stick with Char(1) and just look at the first
character in the string.
 
Tks Jon...I'm using the Convert.ToChar(...ExecuteScalar()); and is
everything ok...i'm still understanding when should i use convert, cast,
etc... =/

tks a lot!
 
This should work, so long as it's a char(1) field and no a varchar(1) that
might be empty:

char chrTipPessoa = ((string)sqlCmd.ExecuteScalar())[0];

Of course if it's a field that might contain a NULL then you'll have to do
something like the following, which would substitute a space for a NULL:

object result = sqlCmd.ExecuteScalar();
char chrTipPessoa = result is DBNull.Value?' ':((string)result)[0];

--Bob
 
Depending on the datatype of the table , ExecuteScalar may return
everything, string, char or even an array.
Just to make sure use ToString() to convert the result into a string:

char chrTipoPessoa = sqlCmd.ExecuteScalar().ToString();
 
Back
Top