473,785 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specified cast is not valid

I declare variable as int16:

int16 hourS;

then I open dataReader and set the variable:

hourS=rdr.getIn t16(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.T oint16(rdr.getI nt16(1));

it works, but doesn't make sense.

Regards,S
Nov 17 '05 #1
7 12033
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.getIn t16(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.T oint16(rdr.getI nt16(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.getIn t32(1);

or:

int16 hourS;
hourS=convert.T oint16(rdr.getI nt32(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*******@disc ussions.microso ft.com> wrote in message
news:68******** *************** ***********@mic rosoft.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.getIn t16(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.T oint16(rdr.getI nt16(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*******@disc ussions.microso ft.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.co m>
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,get date())

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.T oint16(rdr.getI nt32(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)(h ourS+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.getIn t32(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)(h ourS+1)

and i still use procesor work with calling conversion functions:

hourS=Convert.T oInt16(rdr.getI nt32(1));

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

Simon

"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> wrote in message
news:OS******** ********@TK2MSF TNGP15.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.T oint16(rdr.getI nt32(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)(h ourS+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.getIn t32(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
3640
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: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
4
7043
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
10519
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 columns is a template column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here is the code that is causing it as well as the error (below). ERROR...
2
3093
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: try {
3
2185
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
623
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 not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> System.InvalidCastException: Specified...
0
1627
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. ----------------------------------------------------------- Server VS.NET 2003 SQLServer 2000 IIS 66.0
8
4272
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 not valid"). How do I fix it ? using System.Diagnostics; .. .. class NewProcess: Process {
3
12436
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: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
2
8785
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; Microsoft.Web.UI.WebControls.TreeNode iNode = null;
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.