Marcin,
I'm not sure why you are looking to serialize the SqlParameter itself. The
error you are getting is caused by the SqlParameter class not being
designated as serializable. This class is part of the .NET Framework so you
are not able to change it to be serializable (only Microsoft could do so).
However that shouldn't matter because if you are attempting to store the
serialized data in the stored procedure parameter you just have to serialize
the data into a string or binary and not the SqlParameter. For example let's
say you have a class
[Serializable()]
public class MyData
{
private int _myInt = 0;
public int myInt{ get{ return _myInt;} set{ _myInt = value;}}
}
that you would like to store in the database. Notice the [Serializable()]
attribute above the name. That tells the .NET compiler to compile the class
as serializable. You would then serialize an instance of this class
containing the data you are looking to store in the database. You would set
the SqlParameter to that serialized data. I use the following utility
methods I put together to serialize/deserialize the object to and from a
string.
private string StringFromObjec t(object ObjectToSeriali ze)
{
MemoryStream ms = new MemoryStream();
string theSerializedOb ject;
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter
= new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
try
{
formatter.Seria lize(ms, ObjectToSeriali ze);
byte[] thisByteArray = ms.ToArray();
theSerializedOb ject = Convert.ToBase6 4String(thisByt eArray);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Serializing " +
ObjectToSeriali ze.GetType().To String() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}
return theSerializedOb ject;
}
private object StringToObject( string StringToDeseria lize)
{
object thisDeserialize dObject = null;
byte[] thisByteArray = Convert.FromBas e64String(Strin gToDeserialize) ;
MemoryStream ms = new MemoryStream(th isByteArray);
try
{
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter
= new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
thisDeserialize dObject = formatter.Deser ialize(ms);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Deserializing " + StringToDeseria lize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}
return thisDeserialize dObject;
}
Once I've serialized the object to a string I simply set the SqlParameter to
the string. Does this answer your question?
"Marcin" wrote:
Solel Software wrote:
Based on your suggestion I looked into this further and was able to determine the problem. I was truncating the data by setting a limit to the stored procedure parameter:
SqlParameter parameterData = new SqlParameter("@ Data", SqlDbType.NText , 16);
Once I removed the 16 it worked! Thanks for your help.
I would like to ask you how to serialize SqlParameter. When I try it,
I've error:
"The type System.Data.Sql Client.SqlParam eter in Assembly System.Data,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9 is
not marked as serializable."
What I do wrong ?
How do I have to implement this?
Thanks,
Marcin