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 StringFromObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
Logs.WriteToLog("Error Serializing " +
ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}
return theSerializedObject;
}
private object StringToObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}
return thisDeserializedObject;
}
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.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."
What I do wrong ?
How do I have to implement this?
Thanks,
Marcin