Click here to Skip to main content
15,902,901 members
Home / Discussions / C#
   

C#

 
AnswerRe: Sudoku problem Pin
EliottA26-Aug-09 11:21
EliottA26-Aug-09 11:21 
AnswerRe: Sudoku problem Pin
Henry Minute26-Aug-09 14:49
Henry Minute26-Aug-09 14:49 
GeneralRe: Sudoku problem Pin
GravityKoch26-Aug-09 20:26
GravityKoch26-Aug-09 20:26 
QuestionLoading status bar for data in other form !! Pin
rocky81126-Aug-09 9:29
rocky81126-Aug-09 9:29 
AnswerRe: Loading status bar for data in other form !! Pin
I Believe In GOD26-Aug-09 9:58
I Believe In GOD26-Aug-09 9:58 
AnswerRe: Loading status bar for data in other form !! Pin
DaveyM6926-Aug-09 10:04
professionalDaveyM6926-Aug-09 10:04 
QuestionProblem wrting to custom config element Pin
CTaylor8926-Aug-09 9:25
CTaylor8926-Aug-09 9:25 
QuestionDrag and Drop Pin
JimLaVine26-Aug-09 9:07
JimLaVine26-Aug-09 9:07 
AnswerRe: Drag and Drop Pin
kKamel26-Aug-09 11:18
kKamel26-Aug-09 11:18 
GeneralRe: Drag and Drop Pin
JimLaVine26-Aug-09 11:20
JimLaVine26-Aug-09 11:20 
GeneralRe: Drag and Drop Pin
kKamel26-Aug-09 11:22
kKamel26-Aug-09 11:22 
AnswerRe: Drag and Drop Pin
Kevin Marois26-Aug-09 11:51
professionalKevin Marois26-Aug-09 11:51 
QuestionMouse Scheme in windows Pin
caiena26-Aug-09 7:11
caiena26-Aug-09 7:11 
AnswerRe: Mouse Scheme in windows Pin
I Believe In GOD26-Aug-09 9:56
I Believe In GOD26-Aug-09 9:56 
QuestionProgress bar [loading progress bar for background data load ] Pin
smoothcriminal26-Aug-09 6:29
smoothcriminal26-Aug-09 6:29 
AnswerRe: Progress bar [loading progress bar for background data load ] Pin
Henry Minute26-Aug-09 7:12
Henry Minute26-Aug-09 7:12 
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 

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.