473,605 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ConvertToInt16

If I have for example next select statement in SQL stored procedure:

CREATE PROCEDURE test
as
SELECT 1 as dataNumber

If I read this number with dataReader

Int16 dataNumber
dataNumber=rdr. GetInt16(0);

I get an error - invalid cast.

If I use convert function than it works:

dataNumber=Conv ert.ToInt16(rdr .GetInt32(0));

Why? It seems that even If I return small number from database (1 in my
case), it consider it as int32.

I would like to do the same without convert function.

Any idea?

regards,S
Jan 9 '06 #1
5 3245
The problem here is that most databases will use the int (32-bit) SQL
data-type.

If you really want a 16-bit number, you need to let the server know; you
could (under SQL-Server, at least), use:

SELECT CAST(1 as smallint) as dataNumber

Or just live with the Convert ;-p

Marc

"SimonZ" <si*********@st udio-moderna.com> wrote in message
news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
If I have for example next select statement in SQL stored procedure:

CREATE PROCEDURE test
as
SELECT 1 as dataNumber

If I read this number with dataReader

Int16 dataNumber
dataNumber=rdr. GetInt16(0);

I get an error - invalid cast.

If I use convert function than it works:

dataNumber=Conv ert.ToInt16(rdr .GetInt32(0));

Why? It seems that even If I return small number from database (1 in my
case), it consider it as int32.

I would like to do the same without convert function.

Any idea?

regards,S

Jan 9 '06 #2
Hi, Marc

it's really funny.

If I have tinyInt nothing works.

dataNumber=rdr. GetInt16(0);

dataNumber=rdr. GetInt32(0);

dataNumber=rdr. GetSqlInt16(0);

All return error.

It works only:
dataNumber=Conv ert.ToInt16(rdr .GetValue(0));

I haven't try with smallInt. I'll try with that. The problem is that I have
to change dataType in my database because of that.

Regards,
S

"Marc Gravell" <mg******@rm.co m> wrote in message
news:eJ******** ******@TK2MSFTN GP14.phx.gbl...
The problem here is that most databases will use the int (32-bit) SQL
data-type.

If you really want a 16-bit number, you need to let the server know; you
could (under SQL-Server, at least), use:

SELECT CAST(1 as smallint) as dataNumber

Or just live with the Convert ;-p

Marc

"SimonZ" <si*********@st udio-moderna.com> wrote in message
news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
If I have for example next select statement in SQL stored procedure:

CREATE PROCEDURE test
as
SELECT 1 as dataNumber

If I read this number with dataReader

Int16 dataNumber
dataNumber=rdr. GetInt16(0);

I get an error - invalid cast.

If I use convert function than it works:

dataNumber=Conv ert.ToInt16(rdr .GetInt32(0));

Why? It seems that even If I return small number from database (1 in my
case), it consider it as int32.

I would like to do the same without convert function.

Any idea?

regards,S


Jan 9 '06 #3
SimonZ wrote:
If I have tinyInt nothing works.

dataNumber=rdr. GetInt16(0);

dataNumber=rdr. GetInt32(0);

dataNumber=rdr. GetSqlInt16(0);

All return error.


Yes, they would. TinyInt is a byte - so try rdr.GetByte(0).

Jon

Jan 9 '06 #4
Hi,


I haven't try with smallInt. I'll try with that. The problem is that I
have to change dataType in my database because of that.


If you take a look at the SqlDbType enum description in MSDN you will get a
nice table of the type, and the C# type associated. This is the cast that
will work.

In your case TinyInt is a byte, not an int16, the int16 is smallInt.

What is wrong with Convert in the first place?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 9 '06 #5
SimonZ wrote:
If I have for example next select statement in SQL stored procedure:

CREATE PROCEDURE test
as
SELECT 1 as dataNumber

If I read this number with dataReader

Int16 dataNumber
dataNumber=rdr. GetInt16(0);

I get an error - invalid cast.

<snip>

If you're ever wondering what type a value has, get it as an Object and
print out the type of it:

System.Diagnost ics.Debug.Write Line(rdr.GetVal ue(0).GetType() .FullName);

Since GetValue will return DBNull.Value instead of null, doing this
should be safe enough, otherwise you'll need to check for a null result
if you're concerned about that.

Of course, by looking at it through the debugger you can do it much
easier without having to write extra code.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
263
by: Z | last post by:
Hi, If I have this enumeration: public enum SampleEnum { First=1, Second=2, Third=3 }
0
8007
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8428
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
8423
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...
1
8076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8290
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
5446
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3913
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1546
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.