By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,167 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,167 IT Pros & Developers. It's quick & easy.

Specified cast is not valid error

P: n/a
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
Jan 1 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Dec 31, 8:06*pm, "John Rogers" <johnrogers2...@aol.comwrote:
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
Jan 1 '08 #2

P: n/a
On Mon, 31 Dec 2007 19:06:32 -0800, John Rogers <jo************@aol.com>
wrote:
[...]
// 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
Jan 1 '08 #3

P: n/a
>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

Jan 1 '08 #4

P: n/a
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
Jan 2 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.