471,066 Members | 1,020 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Saving serialized data to database problem

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.SerializationExceptio n - 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.
Nov 17 '05 #1
0 2577

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by timb | last post: by
10 posts views Thread by Fabuloussites | last post: by
5 posts views Thread by Ben Kim | last post: by
9 posts views Thread by PI | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.