Solved

Deserialization from Varbinary field

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with opening combobox using VB.NET. 3 25
Sending an input value from a view to a controller in MVC 3 22
abstract class C# 1 31
DataGridView / get bound table name? 8 24
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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