Specified cast is not valid error

J

John Rogers

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
 
M

Matt

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

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.

Matt
 
P

Peter Duniho

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

I get an error of "Specified cast is not valid." [...]

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

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 will
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 the
correct fix might be. You could protect the code with something like this:

object objResult = vCommand.ExecuteScalar();

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

But if the code sample is supposed to work without a change like that, it
probably means there's something wrong with your data instead.

Pete
 
J

John Rogers

But if the code sample is supposed to work without a change like
that, it probably means there's something wrong with your data
instead.

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
 
H

Hans Kesting

John Rogers laid this down on his screen :
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top