473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to serialize SqlParameter ?

hi, I would like to ask how to serialize class with members like
SqlParameter. I try to use BinaryFormatter.

[Serializable]
public class Example_Class
{

public SqlParameter sqlParam;

public Example_Class()
{
sqlParam = new SqlParameter("@Data", SqlDbType.VarChar);
}
}

class App
{
[STAThread]
static void Main()
{
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

try
{
// Construct a BinaryFormatter and use it
// to serialize the data to the stream.
BinaryFormatter formatter = new

Example_Class dod = new Example_Class();
// Serialize the array elements.
formatter.Serialize(fs, dod);

// Deserialize the array elements.
fs.Position = 0;
Example_Class dod2 = (Example_Class) formatter.Deserialize(fs);
Console.WriteLine("Do both class refer to the same object? "
+ (dod.sqlParam == dod2.sqlParam));
Console.ReadLine();

}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
Console.ReadLine();
throw;
}
finally
{
fs.Close();
}
}

and error is :

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

but I know that's possible. What I do wrong ?
How do I have to implement this?

Thanks,
Marcin

Nov 16 '05 #1
1 7477
Marcin,

You would have to mark the field with the SqlParameter as
NonSerializable (with the attribute). Then, you can implement the
IDerializationCallback interface to be notified when deserialization is
complete, so that you can re-initialize it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marcin" <ma*********@poczta.onet.pl> wrote in message
news:cf**********@atlantis.news.tpi.pl...
hi, I would like to ask how to serialize class with members like
SqlParameter. I try to use BinaryFormatter.

[Serializable]
public class Example_Class
{

public SqlParameter sqlParam;

public Example_Class()
{
sqlParam = new SqlParameter("@Data", SqlDbType.VarChar);
}
}

class App
{
[STAThread]
static void Main()
{
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

try
{
// Construct a BinaryFormatter and use it
// to serialize the data to the stream.
BinaryFormatter formatter = new

Example_Class dod = new Example_Class();
// Serialize the array elements.
formatter.Serialize(fs, dod);

// Deserialize the array elements.
fs.Position = 0;
Example_Class dod2 = (Example_Class) formatter.Deserialize(fs);

Console.WriteLine("Do both class refer to the same object? "
+ (dod.sqlParam == dod2.sqlParam));
Console.ReadLine();

}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message); Console.ReadLine();
throw;
}
finally
{
fs.Close();
}
}

and error is :

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

but I know that's possible. What I do wrong ?
How do I have to implement this?

Thanks,
Marcin

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kenneth | last post by:
Can anyone explain me why it is neccesary to include SqlDbType to the SqlParameter. In every example I see, it is done, but no one explaines why. I have for example a date I want to save into my...
2
by: confused | last post by:
Hi, I want to assign a default value of DBNull.Value to my stored procedure parameter, but also have the ability to overwrite it, so: SqlParameter param = new SqlParameter...
5
by: Daniel Groh | last post by:
What is the best way to access a procedure ? MyCommand.Parameters.Add("@param1",typeof(string)); or SqlParameter myParam = new SqlParameter(); myParam.Add("@param1",typeof(string)); What...
4
by: Jason Huang | last post by:
Hi, I want to use the Sqlparameter and SqlDataAdapter to update my data, and the data will be updated based on two TextBoxes txtCustName and txtCustAddress. Thanks for help. Jason
5
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
0
by: Elliot M. Rodriguez | last post by:
I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of...
3
by: Stacey Levine | last post by:
I have a webservice that has the below procedure. Basically a procedure to called a stored procedure and return the results. When I try to call the webservice from my program I get the error. Both...
3
by: Mirek Endys | last post by:
What is the best way to serialize object, that contains data in interfaces. Simple. I have instance of my object, that contains data in interfaces. What is the best way to XMLSerialize and...
6
by: Tim Zych | last post by:
' Declare a new parameter object Dim param() As SqlParameter = New SqlParameter(0) {} ' Set this to null and make it an InputOutput parameter param(0) = New SqlParameter("@Something, DBNull.Value)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.