Groups | Blog | Home
all groups > c# > december 2007 >

c# : Specified cast is not valid error


Matt
12/31/2007 7:20:31 PM
[quoted text, click to view]

ExecuteScalar returns an object which refers to the first value
returned by the call. I'd have to assume that NodeID was not a valid
integer or it was null.

Peter Duniho
12/31/2007 7:27:11 PM
On Mon, 31 Dec 2007 19:06:32 -0800, John Rogers <johnrogers2345@aol.com>=
=

[quoted text, click to view]

Well, an error casting is always related to the source data type being =

incompatible with the destination.

In this case, the method return type is "object", so the only time that =
=

you would be able to successfully cast the result to "int" is when the =

result is an "int". If it's null, or some other data type, the cast wil=
l =

fail.

So, in this case either the serial # being requested from the database =

just doesn't exist, or the "NodeID" column isn't returning an Int32.

Without knowing anything about the exact sample, it's hard to say what t=
he =

correct fix might be. You could protect the code with something like th=
is:

object objResult =3D vCommand.ExecuteScalar();

if (objResult !=3D null && objResult is int)
{
int vRootNode =3D (int)objResult;
}

But if the code sample is supposed to work without a change like that, i=
t =

probably means there's something wrong with your data instead.

John Rogers
12/31/2007 9:06:32 PM
Can someone help me out if possible with this error when running a
program.

I have this code in the program
//-------------------------------------------
System.Data.OleDb.OleDbCommand vCommand = mConnection.CreateCommand();

string vSql = "INSERT INTO [Nodes]
(ParentID,aName,Description,R,G,B,Serial) " +
"VALUES(-1,'" + iName + "','" + iDescription + "'," + iRed.ToString()
+ "," + iGreen.ToString() +
"," + iBlue.ToString() + ",'" + iSerial + "')";

vCommand.CommandText = vSql;
vCommand.ExecuteNonQuery();

vSql="SELECT [NodeID] FROM [Nodes] WHERE [Serial]='" + iSerial + "'";
vCommand.CommandText=vSql;

// crashes on this line
int vRootNode = (int)vCommand.ExecuteScalar();
//-----------------------------------------------------

I get an error of "Specified cast is not valid." This is not my
program, I am trying to learn from it,
but its not going too well since I have to be fixing errors. I got
this project off of codeproject to
work with.

Is anyone able to tell what the problem is by looking at these few
lines of code?


Thanks
John

John Rogers
12/31/2007 10:21:23 PM
[quoted text, click to view]

I will definitely give what you said a shot, but it could be a problem
with the
data like you mentioned. The sample did not come with an access db,
so I had
to create one with the information that i saw in the file. Maybe you
can tell me
if it's correct or not.

//----------------------------------------------------------------------------
vCommand.CommandText="DELETE FROM [Nodes] WHERE [NodeID]=" +
iNodeID.ToString();

vCommand.CommandText="DELETE FROM [Nodes] WHERE [RootID]=" +
iRootID.ToString();

AddRootNode(string iName, string iDescription, int iRed, int iGreen,
int iBlue, string iSerial)

string vSql = "INSERT INTO [Nodes]
(ParentID,aName,Description,R,G,B,Serial)
//----------------------------------------------------------------------------

Every line there is from different parts of the code, but I created
the DB like this.

Table name: Nodes
NodeID, ParentID, RootID, R, G, B = number
aName, Description, Serial = text

I wish the thing had a sample db with it, but I guess you can't have
everything.

This is the article here:
http://www.codeproject.com/KB/cs/treebuilder.aspx


Thanks again for all the help guys.

Oh yea, while I was debugging and getting the error. The Serial value
does have a number,
but in the debugger popup window with the error, it's colored red.

John


Hans Kesting
1/2/2008 10:00:07 AM
John Rogers laid this down on his screen :

[quoted text, click to view]

The red color just means "this value has changed".
The last column in that window shows the real type. Is that "Int32" (or
"int") or something else (Double, Int64)?

You can only cast "object" to "int" if it really is an int.

this works:
int i=3;
object o = i;
int i2 = (int)o;

this fails:
double d = 3.6;
object o = d;
int i2 = (int) o;

this will work:
double d = 3.6;
object o = d;
int i2 = (int)(double)o;

Hans Kesting

AddThis Social Bookmark Button