byte?[] to byte[] Need Help!

byte?[] to byte[] Need Help!

Old forum URL: forums.lhotka.net/forums/t/4802.aspx


Liza posted on Monday, May 05, 2008

Two questions really.

1.  I have a byte[] field called _recurrence (for purposes of infragistics scheduler) that I need to convert to byte?[].  How do you convert from byte[] to byte?[] and vice versa?

2. In the safe data reader, I get binary array like _recurrence = (byte[])dr["Recurrence"];  How can I do this with byte?[]?

Thanks.

Liz.

PS.  I hate doing it like this but there really is no other choice!

tmg4340 replied on Tuesday, May 06, 2008

Liza -

Before answering your questions, maybe you can provide some clarification.

I am guessing that what you're doing is creating an instance of the Recurrence class and serializing that directly to the database.  Is that correct?  If so, I'm a little surprised that works - I didn't think the Recurrence class would serialize.  But that's a different discussion.

Based on your second question, I'm wondering if the real problem you're having is that not all records in your database table have a recurrence associated with them, and so the field is returning NULL.  If so, switching from a "byte[]" to a "byte?[]" won't help you.  A NULL field in the database is different than a binary field containing an array of nullable-byte types.

You might want to investigate the "GetBytes" method of the datareader - that returns the number of bytes read from the field, and will return a 0 for a NULL field.  That would probably allow you to handle the NULL condition more easily.

Having said all that, as far as I know, if you want to convert a byte[] to a byte?[], you'll have to do it manually - i.e. loop through the original byte[] array and copy each value to the byte?[] array.  And as for the SafeDataReader, you could do just what you've done here with the new type:

_recurrence = (byte?[])dr["Recurrence"];

But I'm guessing that cast doesn't work, for the reasons I've outlined above.  So you would be back to the manual copy again.

HTH

- Scott

Copyright (c) Marimer LLC