Unable to cast object of type while deserializing to class

W

webq123

Hi,
I am running into this problem while deserializing the class, I do not
get this error the first time I try to deserialize but then I keep
getting this error - Unable to cast object of type ABCSettings to type
ABCSettings.

I tried closing the memory stream but no luck.Can you please help me
figure out this issue

byte[] b = Convert.FromBase64String(szSettingsSerialized);
MemoryStream oMs2 = new MemoryStream(b);
BinaryFormatter oBinaryFormatter = new BinaryFormatter();
if(this.zSettings == null)
this.xSettings = new ABCSettings()
try
{
this.xSettings =
(ABCPage.ABCSettings)oBinaryFormatter.Deserialize(oMs2);
}
catch(Exception ex)
{
this.aErrorMessages.Add("An error occurred.[" + ex.Message + "]");
}
finally
{
oMs2.Close();
}
 
W

webq123

The error is thrown at
this.xSettings =
(ABCPage.ABCSettings)oBinaryFormatter.Deserialize(oMs2);

please ignore the typo at if(this.zSettings == null)
 
D

Daniel

Do this. before you deserialise:

oMs2.Position = 0;

Let me know....

The error is thrown at
this.xSettings =
(ABCPage.ABCSettings)oBinaryFormatter.Deserialize(oMs2);

please ignore the typo at if(this.zSettings == null)


Hi,
I am running into this problem while deserializing the class, I do not
get this error the first time I try to deserialize but then I keep
getting this error - Unable to cast object of type ABCSettings to type
ABCSettings.

I tried closing the memory stream but no luck.Can you please help me
figure out this issue

byte[] b = Convert.FromBase64String(szSettingsSerialized);
MemoryStream oMs2 = new MemoryStream(b);
BinaryFormatter oBinaryFormatter = new BinaryFormatter();
if(this.zSettings == null)
this.xSettings = new ABCSettings()
try
{
this.xSettings =
(ABCPage.ABCSettings)oBinaryFormatter.Deserialize(oMs2);
}
catch(Exception ex)
{
this.aErrorMessages.Add("An error occurred.[" + ex.Message + "]");
}
finally
{
oMs2.Close();
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top