Home All Groups Group Topic Archive Search About

SerlializationException

Author
17 Nov 2006 12:24 AM
lord.zoltar
I am serializing a some objects and inserting them into a database,
then retrieving them afterwards.
Serializing (using a BinaryFormatter) and storing the objects seems to
give me no problems, but when I try to deserialize, I get  "The input
stream is not a valid binary format." exception (followed by a long
string of bytes that represent the object and it seems to be correct).
What is really puzzling me is that I ran a test and when I deserialize
the objects immediately (that is, I don't store them to the database, I
just deserialize them right away), everything seems to work fine. I
only seem to have problems when one procedure serializes the object and
stores it in the database, and another one retreives it from the
database and deserializes it.

Oh yeah, the database I am using is SQL Server 2005, and the objects
are stored in fields of type varbinary(max).

Are there any experts out there on VB Serialization who could help? I'm
still kinda new to this...

thanks. ;)

Author
17 Nov 2006 6:00 AM
Tom Shelton
lord.zol***@gmail.com wrote:
Show quoteHide quote
> I am serializing a some objects and inserting them into a database,
> then retrieving them afterwards.
> Serializing (using a BinaryFormatter) and storing the objects seems to
> give me no problems, but when I try to deserialize, I get  "The input
> stream is not a valid binary format." exception (followed by a long
> string of bytes that represent the object and it seems to be correct).
> What is really puzzling me is that I ran a test and when I deserialize
> the objects immediately (that is, I don't store them to the database, I
> just deserialize them right away), everything seems to work fine. I
> only seem to have problems when one procedure serializes the object and
> stores it in the database, and another one retreives it from the
> database and deserializes it.
>
> Oh yeah, the database I am using is SQL Server 2005, and the objects
> are stored in fields of type varbinary(max).
>
> Are there any experts out there on VB Serialization who could help? I'm
> still kinda new to this...
>
> thanks. ;)

Just a guess, but I think there is something wrong with either you
storage or retrieval code.  In other words, I don't think you getting
it all back from the db.  Can you post some minimal code that
demonstrates the whole process (storage and retrieval) that gets the
error?

--
Tom Shelton
Author
17 Nov 2006 6:10 AM
Cor Ligthert [MVP]
Tom,

I thought will I answer it, but than I thought, I should wait, maybe Tom
comes around, this is his part in this newsgroup.

Is it not awfull late for you?

Cor

Show quoteHide quote
"Tom Shelton" <tom_shel***@comcast.net> schreef in bericht
news:1163743204.438302.246640@e3g2000cwe.googlegroups.com...
>
> lord.zol***@gmail.com wrote:
>> I am serializing a some objects and inserting them into a database,
>> then retrieving them afterwards.
>> Serializing (using a BinaryFormatter) and storing the objects seems to
>> give me no problems, but when I try to deserialize, I get  "The input
>> stream is not a valid binary format." exception (followed by a long
>> string of bytes that represent the object and it seems to be correct).
>> What is really puzzling me is that I ran a test and when I deserialize
>> the objects immediately (that is, I don't store them to the database, I
>> just deserialize them right away), everything seems to work fine. I
>> only seem to have problems when one procedure serializes the object and
>> stores it in the database, and another one retreives it from the
>> database and deserializes it.
>>
>> Oh yeah, the database I am using is SQL Server 2005, and the objects
>> are stored in fields of type varbinary(max).
>>
>> Are there any experts out there on VB Serialization who could help? I'm
>> still kinda new to this...
>>
>> thanks. ;)
>
> Just a guess, but I think there is something wrong with either you
> storage or retrieval code.  In other words, I don't think you getting
> it all back from the db.  Can you post some minimal code that
> demonstrates the whole process (storage and retrieval) that gets the
> error?
>
> --
> Tom Shelton
>
Author
17 Nov 2006 7:25 AM
Tom Shelton
Cor Ligthert [MVP] wrote:
> Tom,
>
> I thought will I answer it, but than I thought, I should wait, maybe Tom
> comes around, this is his part in this newsgroup.
>
> Is it not awfull late for you?
>

I guess.  I am trying to be "around" more - but, things seem to be not
going my way the last year of so, when it comes to time for the news
groups :)  About a year ago, we bought an old farm house and it has
been really keeping me busy when I'm not at work.  Also, I just changed
jobs after eight years with my last employer - boy was that traumatic
(but good)!  So, I come around when I can.  And answer what I have time
for - which just isn't much lately.

--
Tom Shelton
Author
20 Nov 2006 3:58 PM
lord.zoltar
> Just a guess, but I think there is something wrong with either you
> storage or retrieval code.  In other words, I don't think you getting
> it all back from the db.  Can you post some minimal code that
> demonstrates the whole process (storage and retrieval) that gets the
> error?
>
> --
> Tom Shelton

Here's the serizlization code:
Dim serializer As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim objStream As New System.IO.MemoryStream

serializer.Serialize(objStream, uiObj)
Dim hexStr As String = ""
'convert each serialization to a hex string
For Each b As Byte In objStream.ToArray
    hexStr &= byteToHexString(b) 'quick 'n' dirty function returns hex
rep of a byte.
Next

'now, send each data to the database.
db_query.execute("EXEC insert_blob @name='" & blobName & "', @data=0x"
& hexStr)
-----


and here's the deserialization code:

db_query.execute("EXEC get_saved_blob @name='" & tmpName & "'",
blob_table)
'check to see if there are any parts in the search
If blob_table.Rows.Count > 0 Then
    Dim serializer As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    For Each blob_row As Data.DataRow In blob_table.Rows
        Dim blobStream As New System.IO.MemoryStream
        For Each b As Byte In blob_row.Item("search_blob")
        'write the bytes to the stream.
            blobStream.WriteByte(b)
        Next
        blobStream.Position = 0
        uiPanel.Controls.Add(CType(serializer.Deserialize(blobStream),
Windows.Forms.Control))
    Next
End If

-----

I have a hunch that the code in the deserialization might be broken,
but I'm just not sure.

Thanks.