473,383 Members | 1,717 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,383 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 1541
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; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.