473,385 Members | 1,848 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,385 software developers and data experts.

tinyint, Byte and byte

Hi,

I have a field "Month" in my SQL server table. The type of this field is
"tinyint".

Now what I am doing in the code is using DataRow to read this field in a
'int' variable.

int month = (int) dataRow["Month"];

But the above line throws exception that the cast is invalid.

After some time i got to know that the type of expression dataRow["Month"]
is System.Byte.

In the documentation it has been mentioned that 'byte' is implicitly
converted to 'int' when assigned/compared with a 'int' variable.

I am able to run my code by modifying it to
int month = (byte) dataRow["Month"];

From this, I concluded that the implicit conversion holds only for
byte-to-int and not for Byte-to-int.
Is this a feature of C#/.Net or, Microsoft just forgot to do a implicit
Byte-to-int conversion?

Because now I have a overhead of finding the actual type mapping between SQL
types and C# types.
Otherwise, what I wanted to do was to cast every integer fields (either int
or tinyint) to int of C#, without bothering about the underlying conversion.

-
wa

Nov 16 '05 #1
1 32738
Wasim Akram <a@b.c> wrote:
I have a field "Month" in my SQL server table. The type of this field is
"tinyint".

Now what I am doing in the code is using DataRow to read this field in a
'int' variable.

int month = (int) dataRow["Month"];

But the above line throws exception that the cast is invalid.

After some time i got to know that the type of expression dataRow["Month"]
is System.Byte.
No. The type of that expression is Object. The type of the value
returned by that expression may happen to be System.Byte in your case,
but that's a different matter - the compiler doesn't know that.
In the documentation it has been mentioned that 'byte' is implicitly
converted to 'int' when assigned/compared with a 'int' variable.

I am able to run my code by modifying it to
int month = (byte) dataRow["Month"];

From this, I concluded that the implicit conversion holds only for
byte-to-int and not for Byte-to-int.
Is this a feature of C#/.Net or, Microsoft just forgot to do a implicit
Byte-to-int conversion?
There is no difference between Byte and byte - but there *is* a
difference between a boxed byte and an unboxed byte. You have to
explicitly unbox with the exact type, and *then* you can (implicitly or
explicitly) convert the unboxed value to int.
Because now I have a overhead of finding the actual type mapping between SQL
types and C# types.
Otherwise, what I wanted to do was to cast every integer fields (either int
or tinyint) to int of C#, without bothering about the underlying conversion.


You need to unbox to the correct type first.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

2
by: valmir cinquini | last post by:
Hi everybody I'm newbee in C# and I'm supporting an application where there's a method like following: public int addNews(DateTime dtNews, string strTitle, string strShortText, string...
2
by: tgif | last post by:
Can someone explain the format for converting a .NET datatype (such as string or int) to a database type (such as tinyint, datetime) for use in a SqlParameter.
0
by: Fabio Negri Cicotti | last post by:
I have created into the SQL Server 2 tables parent/child having the parent an autoincrement primary key. E.g.: Order table ordID - tinyint ordName - string(50) Order_Detail table ordID -...
3
by: dbuchanan | last post by:
How do I explicitly cast a string to Byte? Here are my circumstances; The string is from a textbox and could be empty. The Byte is a tinyint in SQL Server 2000 I am using the AddRow method as...
0
by: Elmo Watson | last post by:
I am having all kinds of problems with a Stored Proc I'm trying to run, with the error I'm getting above. First - the way I'm reading it - the database EXPECTS a TinyInt - and it thinks I'm...
2
by: phillip.s.powell | last post by:
For some bizarre reason, each time I insert or update a row in my table `image`, the field value for "image_width" is fixed at 127, no matter what value I use! Here is the schema: CREATE...
7
by: Ronald S. Cook | last post by:
In a database table we will have a column "Round" for which round of feeding the record applies to at a cattle ranch. The value will always be either 1, 2, or 3 since we feed just that many times...
19
by: tshad | last post by:
I have a value in my sql table set to tinyint (can't set to bit). I am trying to move it into a boolean field in my program and have tried: isTrue = (int)dbReader and isTrue =...
21
by: Geoff Cox | last post by:
Hello I have a TINYINT(1) field, group1, in a mysql data base and the value is 1 but php if ($row == "1") { does not work. What should I be writing here?
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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,...
0
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...

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.