Saving serialized data to database problem

Discussion in 'ASP .Net' started by Tommy Christian, Jul 25, 2003.

  1. Hi!

    Anyone who knows about saving serialized data to database,

    coz I have a problem with that. If I just serialize my session data and

    then deserialize it, it works. But when I save it between those actions into

    Oracle database into long row field, it doesn´t. Below is some code.

    Thanks for everyone, if you know what helps.



    'HERE IT SAVE´S SERIALIZED DATA TO DATABASE

    Function Update()

    Dim oSessionDataStream As New MemoryStream()

    oSessionDataStream = Me.Serialize
    'Me.Serialize is serialized data in a stream

    Dim oByteArray(oSessionDataStream.Length - 1) As Byte

    oSessionDataStream.Read(oByteArray, 0, oByteArray.Length)

    Dim sSessionUpdateSql As String = "UPDATE TABLE SET (SDATA) = (?) WHERE
    ID ='" & Me.Id & "'"

    Dim oSessionDataUpdateCommand As OleDb.OleDbCommand = New
    OleDbCommand(sSessionUpdateSql, Me.mWebPage.Database.Connection)

    oSessionDataUpdateCommand.Parameters.Add("@SDATA",
    System.Data.OleDb.OleDbType.Binary, oSessionDataStream.Length).Value =
    oByteArray

    oSessionDataUpdateCommand.ExecuteNonQuery()

    oSessionDataStream.Close()

    End Function



    'HERE WE GET DATA FROM DATABASE

    Public Function Retrieve() As MemoryStream

    Dim sSessionRetrieveSql As OleDbCommand = New OleDbCommand("SELECT SDATA
    FROM TABLE WHERE SID = '" & Me.Id & "'", mWebPage.Database.Connection)

    Dim oReader As OleDbDataReader

    oReader = sSessionRetrieveSql.ExecuteReader()

    oReader.Read()

    Dim b(oReader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte

    oReader.GetBytes(0, 0, b, 0, b.Length)

    oReader.Close()

    Dim oStream As New MemoryStream(b)

    oStream.Write(b, 0, b.Length)

    Return oStream

    End Function



    'HERE WE TRY TO DESERIALIZE DATA.

    Public Function Deserialize(ByVal oStream As MemoryStream) As Session

    Dim oFormatter As New BinaryFormatter()

    oStream.Position = 0

    Return DirectCast(oFormatter.Deserialize(oStream), Session)

    End Function



    'HERE IS THE ERROR WHICH COMES IN THE LAS ROW. (Return
    DirectCast(oFormatter.Deserialize(oStream), Session))

    System.Runtime.Serialization.SerializationException - Binary stream does not
    contain a valid BinaryHeader, 0 possible causes, invalid stream or object
    version change between serialization and deserialization.



    'XML SERIALIZATION GIVES ERROR

    root does not excist.
    Tommy Christian, Jul 25, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcel Balcarek

    storing serialized object in DB problem

    Marcel Balcarek, Apr 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    311
    Steve C. Orr [MVP, MCSD]
    Apr 15, 2004
  2. Francois Gagnon

    Problem with URL encode serialized object

    Francois Gagnon, Mar 2, 2004, in forum: Java
    Replies:
    1
    Views:
    799
    John C. Bollinger
    Mar 2, 2004
  3. Replies:
    0
    Views:
    546
  4. Enter The
    Replies:
    0
    Views:
    340
    Enter The
    Oct 18, 2007
  5. Mr P
    Replies:
    3
    Views:
    437
Loading...

Share This Page