Solved

Deserialization from Varbinary field

Posted on 2008-06-24
2
1,599 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
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell: ForEach ($y in $x.licenses | where {....} 3 53
Problem to Office 1 39
Why is this SQL bringing back extra rows? (parsing XML data) 4 33
Need help with a query 14 34
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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