473,287 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Specified cast is not valid error

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
4 1538
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
4
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Stack Trace:
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
2
by: Kashiefah | last post by:
Hi, I keep on receiving this error when I click on the edit email link from the datagrid:Specified cast is not valid. Description: An unhandled exception occurred during the execution of the...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.