Click here to Skip to main content
15,889,368 members
Home / Discussions / C#
   

C#

 
AnswerRe: Progress bar [loading progress bar for background data load ] Pin
DaveyM6926-Aug-09 7:23
professionalDaveyM6926-Aug-09 7:23 
GeneralRe: Progress bar [loading progress bar for background data load ] Pin
rocky81126-Aug-09 8:12
rocky81126-Aug-09 8:12 
GeneralRe: Progress bar [loading progress bar for background data load ] Pin
Henry Minute26-Aug-09 8:29
Henry Minute26-Aug-09 8:29 
QuestionP2P connections between apps in different LAN environments Pin
Patrick Eckler26-Aug-09 6:21
Patrick Eckler26-Aug-09 6:21 
AnswerRe: P2P connections between apps in different LAN environments Pin
yuemeng7-Apr-10 20:23
yuemeng7-Apr-10 20:23 
GeneralRe: P2P connections between apps in different LAN environments Pin
Patrick Eckler2-May-10 10:14
Patrick Eckler2-May-10 10:14 
QuestionOffice View Component does not set Differrent First Page Header Pin
dptalt26-Aug-09 6:12
dptalt26-Aug-09 6:12 
QuestionSerialize a collection of objects Pin
godfetish26-Aug-09 5:58
godfetish26-Aug-09 5:58 
In C# I need to serialize a List of Constraint objects. Each Constraint has a Group, Field, Condition, and some values depending on the CGF combination. I have been trying to get these serialized so that I can store them, preferably in an ASP.NET cookie. I cannot use Session to maintain the list because I was told we have 2 web servers and Session won't work, so I need to serialize the CList and push it off in a cookie...I'm new to ASP.NET, but I thought skilled in C# and started working on a test app to serialize a List of generic objects based on a sample app here but it is not working properly.

In this example I make a List and add to it a few employees, then try to serialize it and store off in a file. Reset my List and such, then open the file a read the serialized data back to the List.

When using the original project, one employee at a time worked. The original is here: http://www.codeproject.com/KB/cs/objserial.aspx

using System;<br />
using System.IO;<br />
using System.Runtime.Serialization;<br />
using System.Runtime.Serialization.Formatters.Binary;<br />
using System.Collections.Generic;<br />
<br />
namespace MyObjSerial<br />
{<br />
    [Serializable()]	//Set this attribute to all the classes that you define to be serialized<br />
    public class Employee : ISerializable<br />
    {<br />
        public int EmpId;<br />
        public string EmpName;<br />
<br />
        //Default constructor<br />
        public Employee()<br />
        {<br />
            EmpId = 0;<br />
            EmpName = null;<br />
        }<br />
<br />
        //Deserialization constructor.<br />
        public Employee(SerializationInfo info, StreamingContext ctxt)<br />
        {<br />
            //Get the values from info and assign them to the appropriate properties<br />
            EmpId = (int)info.GetValue("EmployeeId", typeof(int));<br />
            EmpName = (String)info.GetValue("EmployeeName", typeof(string));<br />
        }<br />
<br />
        //Serialization function.<br />
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)<br />
        {<br />
            //You can use any custom name for your name-value pair. But make sure you<br />
            // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"<br />
            // then you should read the same with "EmployeeId"<br />
            info.AddValue("EmployeeId", EmpId);<br />
            info.AddValue("EmployeeName", EmpName);<br />
        }<br />
    }<br />
<br />
    //Main class<br />
    public class ObjSerial<br />
    {<br />
        public static void Main(String[] args)<br />
        {<br />
            //Create a new Employee object<br />
            List<Employee> mp = new List<Employee>();<br />
            Employee e = new Employee();<br />
            e.EmpId = 10;<br />
            e.EmpName = "Omkumar";<br />
            mp.Add(e);<br />
            e.EmpId = 11;<br />
            e.EmpName = "this";<br />
            mp.Add(e);<br />
            e.EmpId = 12;<br />
            e.EmpName = "is a";<br />
            mp.Add(e);<br />
            e.EmpId = 13;<br />
            e.EmpName = "test";<br />
            mp.Add(e);<br />
<br />
            mp.Add(e);<br />
<br />
            // Open a file and serialize the object into it in binary format.<br />
            // EmployeeInfo.osl is the file that we are creating. <br />
            // Note:- you can give any extension you want for your file<br />
            // If you use custom extensions, then the user will now <br />
            //   that the file is associated with your program.<br />
            Stream ostream = File.Open("EmployeeInfo.osl", FileMode.Create);<br />
            BinaryFormatter bformatter = new BinaryFormatter();<br />
<br />
            Console.WriteLine("Writing Employee Information");<br />
            bformatter.Serialize(ostream, mp);<br />
            ostream.Close();<br />
<br />
            //Clear mp and bf for further usage.<br />
            mp = null;<br />
            bformatter = null;<br />
<br />
            //Open the file written above and read values from it.<br />
            Stream istream = File.Open("EmployeeInfo.osl", FileMode.Open);<br />
            bformatter = new BinaryFormatter();<br />
<br />
            Console.WriteLine("Reading Employee Information");<br />
            mp = (List<Employee>)bformatter.Deserialize(istream);<br />
            istream.Close();<br />
            foreach (Employee f in mp)<br />
            {<br />
                Console.WriteLine("Employee Id: {0}", f.EmpId.ToString());<br />
                Console.WriteLine("Employee Name: {0}", f.EmpName);<br />
            }<br />
<br />
        }<br />
    }<br />
}<br />


Thanks for any help you might give.

jrk
AnswerRe: Serialize a collection of objects Pin
Henry Minute26-Aug-09 6:04
Henry Minute26-Aug-09 6:04 
GeneralRe: Serialize a collection of objects Pin
godfetish26-Aug-09 6:06
godfetish26-Aug-09 6:06 
GeneralRe: Serialize a collection of objects Pin
Henry Minute26-Aug-09 6:25
Henry Minute26-Aug-09 6:25 
GeneralRe: Serialize a collection of objects Pin
godfetish26-Aug-09 7:20
godfetish26-Aug-09 7:20 
GeneralRe: Serialize a collection of objects Pin
Henry Minute26-Aug-09 7:43
Henry Minute26-Aug-09 7:43 
GeneralRe: Serialize a collection of objects Pin
godfetish26-Aug-09 8:23
godfetish26-Aug-09 8:23 
GeneralRe: Serialize a collection of objects Pin
Henry Minute26-Aug-09 8:30
Henry Minute26-Aug-09 8:30 
QuestionUsing Cascading DropDown Pin
LucBite26-Aug-09 5:33
LucBite26-Aug-09 5:33 
AnswerRe: Using Cascading DropDown Pin
Henry Minute26-Aug-09 6:05
Henry Minute26-Aug-09 6:05 
QuestionPrint the output of HTML Pin
Programm3r26-Aug-09 4:48
Programm3r26-Aug-09 4:48 
AnswerRe: Print the output of HTML Pin
N a v a n e e t h26-Aug-09 5:00
N a v a n e e t h26-Aug-09 5:00 
GeneralRe: Print the output of HTML Pin
Programm3r26-Aug-09 20:33
Programm3r26-Aug-09 20:33 
GeneralRe: Print the output of HTML Pin
N a v a n e e t h27-Aug-09 2:20
N a v a n e e t h27-Aug-09 2:20 
Questionchange Column type of a binded datagridview Pin
baranils26-Aug-09 4:40
baranils26-Aug-09 4:40 
Question[Solved] How to handle a null enum retrieved from web service [modified] Pin
Brian Triplett26-Aug-09 2:56
Brian Triplett26-Aug-09 2:56 
AnswerRe: How to handle a null enum retrieved from web service Pin
N a v a n e e t h26-Aug-09 5:04
N a v a n e e t h26-Aug-09 5:04 
GeneralRe: How to handle a null enum retrieved from web service Pin
Brian Triplett26-Aug-09 5:08
Brian Triplett26-Aug-09 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.