471,570 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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=Convert.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 3175
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*********@studio-moderna.com> wrote in message
news:Oe**************@TK2MSFTNGP14.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=Convert.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=Convert.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.com> wrote in message
news:eJ**************@TK2MSFTNGP14.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*********@studio-moderna.com> wrote in message
news:Oe**************@TK2MSFTNGP14.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=Convert.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.Diagnostics.Debug.WriteLine(rdr.GetValue(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***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

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.