Solved

Deserialization from Varbinary field

Posted on 2008-06-24
2
1,606 Views
Last Modified: 2013-12-16
I'm saving credit info to a varbinary field in SQL server and have no problem with serialization and saving.  However when I try to deserialize using the function below I get the following error at the getit.ExecuteNonQuery().  A stored proc retrieves the ccAuthorization field as an OUTPUT.

 " ex = {"Byte[][1]: the Size property has an invalid size of 0."}

 Does anyone have any suggestions for what I need to do to resolve?  Thanks in advance.


SqlCommand getit = new SqlCommand("sp_AssureBuyRetrieval", conn);
getit.CommandType = System.Data.CommandType.StoredProcedure;
getit.Parameters.Add("@ID", System.Data.SqlDbType.UniqueIdentifier).Value = id;
SqlParameter sqlParam = getit.Parameters.Add("@ccAuthorization", SqlDbType.VarBinary);
getit.Parameters["@ccAuthorization"].Direction = ParameterDirection.Output;
 
getit.ExecuteNonQuery();
 
string test = Convert.ToString(getit.Parameters["@ccAuthorization"].Value);
 
Byte[] returnValue = (Byte[])getit.Parameters["@ccAuthorization"].Value;
 
IFormatter formatter = new BinaryFormatter();
byte[] cereal = (Byte[])getit.Parameters["@ccAuthorization"].Value;
MemoryStream ccAuthorization = new MemoryStream(cereal);
ccAuthorization.Position = 0;
 
obj = (AssureBuyCCprocesser)formatter.Deserialize(ccAuthorization);
                ccAuthorization.Close();

Open in new window

0
Comment
Question by:abedeenf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Christopher Kile earned 250 total points
ID: 21856131
How about adding this:

getit.Parameters["@ccAuthorization"].Size = <size/length listed for this field in the sproc parameter description>;

Otherwise, the size of the parameter might BE zero.
0
 

Author Closing Comment

by:abedeenf
ID: 31471047
Thanks for your suggestion.  I ended up using the following and it works!
   

         MemoryStream ccAuthorization = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();

            try
            {

                formatter.Serialize(ccAuthorization, obj);
                byte[] dit = new byte[ccAuthorization.Length];

                ccAuthorization.Position = 0;
                ccAuthorization.Read(dit, 0, (int)ccAuthorization.Length);
                SqlCommand putit = new SqlCommand("sp_AssureBuyStorage", conn);
                putit.CommandType = System.Data.CommandType.StoredProcedure;
                putit.Parameters.Add("@ccAuthorization", System.Data.SqlDbType.VarBinary).Value = dit;
                putit.Parameters.Add("@ID", System.Data.SqlDbType.UniqueIdentifier).Value = this.id;
                putit.ExecuteNonQuery();
                ccAuthorization.Close();
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question