Solved

Deserialization from Varbinary field

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now