22

I am reading a row from a SQL Server table. One of the columns is of type tinyint.

I want to get the value into an int or int32 variable.

rdr.GetByte(j)
(byte) rdr.GetValue(j)

...seems to be the only way to retrieve the value. But how do I get the result into an int variable?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jtb
  • 347
  • 1
  • 2
  • 7

7 Answers7

27

int value = rdr.GetByte(j);

An explicit cast is not required, because a byte to int is a widening conversion (no possibility of data loss).

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 1
    P.S. This is an implicit cast `byte` to `int` – abatishchev Jun 15 '10 at 15:54
  • 3
    I should have been more specific. That is what I have already tried. I get a "Specified cast is not valid." exception. I have also tried: int value = (int) rdr.GetByte(j) and int value = Convert.ToInt(rdrGetByte(j)) – jtb Jun 15 '10 at 16:08
  • 1
    @jtb: It seems to me then that you're not getting the tinyint column with the index j. What's the exception stack trace? The error must happen inside `GetByte`. – Jordão Jun 15 '10 at 16:27
  • 2
    I agree with @Jordao; it sounds like column `j` is not a `byte`. Try `int value = Convert.ToInt(rdr.GetValue(j));` – Stephen Cleary Jun 15 '10 at 17:07
15

See the documentation for BitConverter.ToInt32 (contains more examples):

byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
Dolph
  • 49,714
  • 13
  • 63
  • 88
  • 4
    The question is asking how to convert a single `byte` to an `int`. `BitConverter` deals with arrays of `byte`s, not single `byte`s. – Stephen Cleary Jun 15 '10 at 15:55
5

Assigning a byte to an int works:

int myInt = myByte;

But maybe you're getting an exception inside IDataRecord.GetByte, in which case you should check that the index you're using to access the data record really points to a tinyint column. You can check the type returned from GetValue. It should be a byte for a tinyint column.

Trace.Assert(rdr.GetValue(j).GetType() == typeof(byte));

Another option is to forego the fragile numeric index altogether:

int myInt = rdr.GetByte(rdr.GetOrdinal(TheNameOfTheTinyintColumn))
Jordão
  • 55,340
  • 13
  • 112
  • 144
2

Casting the byte to int should work just fine:

int myInt = (int) rdr.GetByte(j);

Since C# supports implicit conversions from byte to int, you can alternatively just do this:

int myInt = rdr.GetByte(j);

Which one you choose is a matter of preference (whether you want to document the fact that a cast is taking place or not). Note that you will need the explicit cast if you want to use type inference, or otherwise myInt will have the wrong type:

var myInt = (int) rdr.GetByte(j);
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2
(int)rdr.GetByte(j)
abatishchev
  • 98,240
  • 88
  • 296
  • 433
2

Quick tidbit I ran into as a kind of corner case. If you have an object type that is of type System.Byte, you can not directly cast to int. You must first cast to a byte, then cast to an int.

public int Method(object myByte)
{
    // will throw a cast exception
    // var val = (int)myInt;

    // will not throw a cast exception
    var val = (int)((byte)myInt)
    return val;
}

Method((byte)1);
Paul Carlton
  • 2,785
  • 2
  • 24
  • 42
0

This is similar to Stephen Cleary's comment on the accepted answer, however I am required to specify the size of the int. This worked for me:

int value = Convert.ToInt32(rdr.GetValue(j));

(And it also provided backward compatibility with a database column using an int.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JoshSub
  • 499
  • 1
  • 4
  • 11