views:

344

answers:

1

Hi,

how do I retrieve the byte array stored in an integer column in my database?

Do I first case it as int, then as byte[] ?

byte[] permissions = (byte) Convert.ToInt(dr["myField"]);

?

A: 

Well, the data is stored as an integer. How do you want to convert that into a byte array. Given an input of, say, 12345, what should the output be?

Why are you trying to store a byte array in an integer column in the first place?

EDIT: Now that we know that it's basically flags you're after, I wouldn't convert it to a byte array, I'd use an enum:

[Flags]
public enum Permissions
{
    Read = 1,
    Write = 2,
    Execute = 4,
    Guillotine = 8,
    Lynch = 16
    // etc
}

Then you can just cast:

Permissions permissions = (Permissions) (int) dr["Field"]
Jon Skeet
I am using it as for flags, to set about 4-5 permission values. S I will be using 0, 1, 2, 4, 8, 16, 32, ...Does that make sense?
Blankman
thanks, that's how I did it before but wasn't sure how I was casting it.
Blankman
There are two casts there - one to unbox the value in the DataRow to an int, and one to convert the int to the enum type.
Jon Skeet