Solved

Deserialization from Varbinary field

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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