473,387 Members | 3,821 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,387 software developers and data experts.

Specified cast is not valid

I declare variable as int16:

int16 hourS;

then I open dataReader and set the variable:

hourS=rdr.getInt16(1);

but I get an error when I start the page:

"Specified cast is not valid."

I don't understand why?
Both sides are defined as int16, also the value which reader returns is
small, like 5 for example.

I know, I can use convert function:

hourS=convert.Toint16(rdr.getInt16(1));

it works, but doesn't make sense.

Regards,S
Nov 17 '05 #1
7 11986
The GetInt16() function returns 'short' which is a C# datatype whereas Int16
is a .net framework's common data type. Although the sizes of both data types
may be the same they are considered as different data types since C# supports
strong type checking.
--
Thanks,
Sameeksha
MCAD.Net
"simon" wrote:
I declare variable as int16:

int16 hourS;

then I open dataReader and set the variable:

hourS=rdr.getInt16(1);

but I get an error when I start the page:

"Specified cast is not valid."

I don't understand why?
Both sides are defined as int16, also the value which reader returns is
small, like 5 for example.

I know, I can use convert function:

hourS=convert.Toint16(rdr.getInt16(1));

it works, but doesn't make sense.

Regards,S

Nov 17 '05 #2
Thank you.

What is the best solution here?

One option is to declare variable as int32 and no conversion function
needed(sql always returns int32):

int32 hourS;
hourS=rdr.getInt32(1);

or:

int16 hourS;
hourS=convert.Toint16(rdr.getInt32(1));

So, in first example I don't need to call convert function but on the other
hand, I spent twice as much memory as with int16.

When work with int16 type there is also one other problem:

int16 a,b;

b=1;
a=b+1; //this wont work

I must always write int16 in front of:
a=(int16)(b+1);

When using int32(or int), everything works:

int32 a,b;
b=1;
a=b+1;

So, what do you suggest?

Work with short inseat of int16 in my example?

regards,
Simon
"Sameeksha" <Sa*******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
The GetInt16() function returns 'short' which is a C# datatype whereas
Int16
is a .net framework's common data type. Although the sizes of both data
types
may be the same they are considered as different data types since C#
supports
strong type checking.
--
Thanks,
Sameeksha
MCAD.Net
"simon" wrote:
I declare variable as int16:

int16 hourS;

then I open dataReader and set the variable:

hourS=rdr.getInt16(1);

but I get an error when I start the page:

"Specified cast is not valid."

I don't understand why?
Both sides are defined as int16, also the value which reader returns is
small, like 5 for example.

I know, I can use convert function:

hourS=convert.Toint16(rdr.getInt16(1));

it works, but doesn't make sense.

Regards,S

Nov 17 '05 #3

"Sameeksha" wrote...
The GetInt16() function returns 'short' which is a C# datatype
whereas Int16 is a .net framework's common data type. Although the sizes of both data types
may be the same they are considered as different data types since
C# supports strong type checking.


Hold your horses!

"Int16" and "short" are the same datatype. "short" is simply an alias for
Int16 and are considered the *same* datatype.
// Bjorn A

Nov 17 '05 #4

"simon" wrote...
Thank you.
Wait a minute before you thank Sameeksha, as he's actually wrong.

"short" and Int16 are simply the same datatype, so your problem is not
there.

In the framework Int16 is the datatype, and "short" is an alias for that
datatype.

So which one you're writing in the source code doesn't really matter, but
*casing* does matter!

There's no type "int16"...
What is the best solution here?


I would suggest that you use "short" instead of "Int16", *but* that's if it
really is the right type to use in your explicit scenario.

My guess is that the error of "casting" actually isn't related to *that*
casting, but to the internal casting done when you do the *reading*.

What DB are you using, and what datatype does the field have.

E.g. if the field is of type "byte" in an Access-db, you can get the casting
error if you try to read it with GetInt16 instead of GetByte.
// Bjorn A
Nov 17 '05 #5
Sameeksha <Sa*******@discussions.microsoft.com> wrote:
The GetInt16() function returns 'short' which is a C# datatype whereas Int16
is a .net framework's common data type. Although the sizes of both data types
may be the same they are considered as different data types since C# supports
strong type checking.


No, that's not true - they are absolutely the same type. "short" is
just a shorthand for Int16, as per the C# specification, section 11.1.3
in ECMA numbering.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Thank you Bjorn for your explanation.

I know about casing and it's not the problem. It's just my inconsistent when
writing to this post.

In code I have Int16.

My select statement is like this for example:

SELECT datepart(hh,getdate())

And select returns sql int data type, which is the same as Int32 in C#.

But number is always small, from 0 to 23, so why use Int32 dataType and use
additional memory?

Because of that, I use Int16 dataType:

Int16 hourS;
hourS=Convert.Toint16(rdr.getInt32(1));

It works.

But it brings a lot of work.

Everywhere in code where I use Int16 variables I must write (Int16) in front
of line, for example:

hours=(Int16)(hourS+1);

If i work with Int32 variables, no additional coding is needed:

Int32 hourS;
hourS=hourS+1;

and also no call to conversion function is needed (so, less processor work):

hourS=rdr.getInt32(1);

So these are the reasons to use Int32 DataType.

You suggest short?

short hourS;

Why?

If i use short:

hourS=hourS+1;

I also get an error:

"Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?) "

So; I still have to write like this:

hourS=(short)(hourS+1)

and i still use procesor work with calling conversion functions:

hourS=Convert.ToInt16(rdr.getInt32(1));

So, in this case the Short is the right answer?

Simon

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:OS****************@TK2MSFTNGP15.phx.gbl...

"simon" wrote...
Thank you.


Wait a minute before you thank Sameeksha, as he's actually wrong.

"short" and Int16 are simply the same datatype, so your problem is not
there.

In the framework Int16 is the datatype, and "short" is an alias for that
datatype.

So which one you're writing in the source code doesn't really matter, but
*casing* does matter!

There's no type "int16"...
What is the best solution here?


I would suggest that you use "short" instead of "Int16", *but* that's if
it really is the right type to use in your explicit scenario.

My guess is that the error of "casting" actually isn't related to *that*
casting, but to the internal casting done when you do the *reading*.

What DB are you using, and what datatype does the field have.

E.g. if the field is of type "byte" in an Access-db, you can get the
casting error if you try to read it with GetInt16 instead of GetByte.
// Bjorn A

Nov 17 '05 #7

"simon" wrote...
Thank you Bjorn for your explanation.
You're welcome.
And select returns sql int data type,
which is the same as Int32 in C#.
Okej, so you should use GetInt32 as that *is* what you're reading from the
db...
But number is always small, from 0 to 23, so why use
Int32 dataType and use additional memory?
Because you can't change the datatype of what you're reading... ;-)

Well, actually I'd guess that you possibly *could* do that in some way, if
you encapsulate the reading in a stored procedure in the db, which in turn
"casts" it somehow to a short instead of an int, but my guess is that it
would be overkill...
Because of that, I use Int16 dataType:

Int16 hourS;
hourS=Convert.Toint16(rdr.getInt32(1));

It works.
This should probably also work:

Int16 hours = (Int16) rdr.getInt32(1);

or

short hours = (short) rdr.getInt32(1);
But it brings a lot of work.
Not explicitly that, but other things might...
Everywhere in code where I use Int16 variables I must
write (Int16) in front of line, for example:

hours=(Int16)(hourS+1);

If i work with Int32 variables, no additional coding is needed:

Int32 hourS;
hourS=hourS+1;

and also no call to conversion function is needed (so, less processor
work):

hourS=rdr.getInt32(1);
Yes and No.

If hourS is declared as an Int16, hourS is *always* an Int16, no matter how
you assigned the value to it. However, the problem *there* lies in what type
a *literal* is of...

In your code:

hourS = hourS + 1;

....whether or not hourS is of type short (Int16), you also have the literal
1, which default is of type int (Int32)! Therefore, you *still* need to cast
the expression if you use short variables.
So these are the reasons to use Int32 DataType.

You suggest short?


As I said before, Int16 and "short" are interchangable in your code, as
short simply is an alias for Int16.

Personally, I prefer short before Int16, because I switch a lot between C#,
C++ and Java. It makes a lot of things easier for me...

In this specific case, I would suggest you use int instead of Int32.

That's the only way you can eliminate the casts of such expressions as
mentioned above...

If you think that it would make your program "too big" because of excessive
use of memory, then you're stuck with the casting, when you use such
literals in the expressions...
// Bjorn A
Nov 17 '05 #8

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....
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
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; ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.