CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #1
    Join Date
    Mar 2006
    Posts
    38

    Serializtion problem in C#

    Hi Every Body
    I tried to write phone index Application,but i got a problem with the serialization of my object,and i don't know why can some body help please and below is my code in detail and i attached the error message

    main form code
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    
    
    namespace PHIndex
    {
    public partial class MainFrm : Form
    {
    
    CTeleRecord phoneRecord;
    
    public MainFrm()
    {
    InitializeComponent();
    
    phoneRecord = new CTeleRecord();
    
    phoneRecord.errorEvnt += new errorEventHandler(ErrorHnd);
    
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    phoneRecord.GSName = textBox1.Text;
    phoneRecord.GShomeNumber = textBox2.Text;
    FileStream outPutSt = new FileStream("test.bin", FileMode.Create, FileAccess.Write);
    BinaryFormatter outBinFormater = new BinaryFormatter();
    outBinFormater.Serialize(outPutSt, phoneRecord);
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
    BinaryFormatter inpuBinFormater = new BinaryFormatter();
    FileStream inPutSt = new FileStream("test.bin", FileMode.Open, FileAccess.Read);
    phoneRecord = (CTeleRecord)inpuBinFormater.Deserialize(inPutSt);
    textBox1.Text= phoneRecord.GSName;
    textBox2.Text= phoneRecord.GShomeNumber;
    }
    void ErrorHnd(object sender, myEventsArg e)
    {
    MessageBox.Show(e.GetErrorMessage,"Error Code"+e.GetErrorCode);
    }
    
    }
    }

    and the telephone Record Class code
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PhoneIndex;
    
    namespace PHIndex
    {
    ///class for holding telephone index object
    public delegate void errorEventHandler(object sender, myEventsArg e);
    
    [Serializable]
    class CTeleRecord
    
    {
    
    
    string name;
    string homeNumber;
    string mobileNumber;
    string workNumber;
    string emailAddress;
    bool errorFlag;
    public event errorEventHandler errorEvnt;
    
    /// <summary>
    /// This Fn will fire the error event
    /// </summary>
    /// <param name="e"></param>
    void fireError(myEventsArg e)
    {
    if (errorEvnt != null)
    {
    ///invoke the delegate object
    errorEvnt(this,e);
    }
    }
    
    
    /// <summary>
    /// reset error flag
    /// </summary>
    public void ResetErrorFlag()
    { errorFlag = false; }
    
    public int getRecordLength()
    {
    
    
    return (name.Length + homeNumber.Length + mobileNumber.Length + workNumber.Length
    + emailAddress.Length);
    
    }
    
    public void resetTelRecordObj()
    {
    name = "";
    homeNumber = "";
    mobileNumber = "";
    workNumber = "";
    emailAddress = "";
    }
    
    /// <summary>
    /// Property for Get or Set home phone number
    /// </summary>
    public string GShomeNumber
    {
    get
    {
    return homeNumber;
    }
    set
    {
    if (value.Length > 0)
    try
    {
    //This property for checking validation of number;
    Convert.ToInt64(value);
    }
    
    catch (FormatException)
    {
    
    //errorFlag = true;
    
    myEventsArg e1 = new myEventsArg(1, "Numbers can't be a string");
    fireError(e1);
    }
    
    try
    {
    if (value.Length == 0 || value.Length == 8 || value.Length == 10 || value.Length == 13)
    homeNumber = value;
    else
    throw new Exception("Invalid Home Telephone Number Length (8 or 10 or 13 number)");
    }
    catch (Exception)
    {
    myEventsArg e11 = new myEventsArg(1, "Invalid Home Telephone Number Length");
    fireError(e11);
    }
    
    }
    }
    
    /// <summary>
    /// Property for Get or Set work telephone number
    /// </summary>
    public string GSWorkNumber
    {
    get{return workNumber;}
    
    set
    {
    if (value.Length > 0)
    try
    {
    //This property for checking validation of number;
    Convert.ToInt64(value)
    ;
    }
    
    catch (FormatException)
    {
    
    myEventsArg e3 = new myEventsArg(3, "Numbers can't be a string");
    fireError(e3);
    }
    
    try
    {
    if (value.Length == 0 || value.Length == 8 || value.Length == 10 || value.Length == 13)
    workNumber = value;
    else
    throw new Exception("Invalid Work Telephone Number Length (8 or 10 or 13 number)");
    }
    catch (Exception error)
    {
    
    myEventsArg e33 = new myEventsArg(3, "Invalid Work Telephone Number Length");
    fireError(e33);
    
    }
    
    }
    }
    
    /// <summary>
    /// Property for Get or Set mobile phone number
    /// </summary>
    public string GSmobileNumber
    {
    get
    {
    return mobileNumber;
    }
    set
    {
    if (value.Length > 0)
    try
    {
    //This property for checking validation of number;
    Convert.ToInt64(value)
    ;
    }
    
    catch (FormatException)
    {
    
    myEventsArg e2 = new myEventsArg(2, "Numbers can't be a string");
    fireError(e2);
    }
    
    try
    {
    if (value.Length == 0 || value.Length == 10 || value.Length == 13)
    mobileNumber = value;
    else
    throw new Exception("Invalid Home Telephone Number Length (8 or 10 or 13 number)");
    }
    catch (Exception)
    {
    
    myEventsArg e22 = new myEventsArg(2, "Invalid Home Telephone Number Length");
    fireError(e22);
    }
    }
    }
    /// <summary>
    /// Property for Get or Set user name
    /// </summary>
    public string GSName
    {
    get
    {
    return name;
    }
    
    set
    {
    try
    {
    if (value.Length == 0 || value.Length <= 50)
    name = value;
    else
    throw new Exception("Invalid Name Length (Max 50 Character)");
    }
    catch (Exception)
    {
    
    myEventsArg e0 = new myEventsArg(0, "You Wrote " + value.Length + "char" + " (Max 30 Character)");
    fireError(e0);
    }
    }
    }
    /// <summary>
    /// Property for Get or Set user email
    /// </summary>
    
    public string GSEmail
    {
    get
    {
    return emailAddress;
    }
    set
    {
    try
    {
    if (value.Contains('@') && value.Contains('.') || value.Contains(""))
    emailAddress = value;
    
    else
    throw new Exception("Invalid Email format");
    }
    
    catch (Exception)
    {
    
    myEventsArg e4 = new myEventsArg(4, "Invalid Email format");
    fireError(e4);
    }
    }
    }
    /// <summary>
    /// get error flag status
    /// </summary>
    public bool ErrorFlag
    { get { return errorFlag; } }
    
    /// <summary>
    /// Return the Record Length
    /// </summary>
    public int GetRecordLength
    {get{ return getRecordLength(); }}
    
    
    }
    }
    and event argument class code is
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace PHIndex
    {
    public class myEventsArg : EventArgs
    {
    public myEventsArg(int ercode,string erCodeMesage)
    {
    errorCode = ercode;
    errorMessage = erCodeMesage;
    }
    private int errorCode;
    private string errorMessage;
    
    public int GetErrorCode
    {get { return errorCode; }}
    
    public string GetErrorMessage
    {get{return errorMessage;}}
    }
    }
    the error in detail
    Code:
    
    System.Runtime.Serialization.SerializationException was unhandled
      Message="Type 'PhoneIndex.Form1' in Assembly 'PhoneIndex, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable."
      Source="mscorlib"
      StackTrace:
           at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
           at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
           at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
           at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
           at PhoneIndex.Form1.Save() in E:\VC++  & VC# Projects\PhoneIndex\PhoneIndex\Form1.cs:line 264
           at PhoneIndex.Form1.saveToolStripMenuItem_Click(Object sender, EventArgs e) in E:\VC++  & VC# Projects\PhoneIndex\PhoneIndex\Form1.cs:line 225
           at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
           at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
           at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
           at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
           at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
           at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
           at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
           at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ToolStrip.WndProc(Message& m)
           at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at PhoneIndex.Program.Main() in E:\VC++  & VC# Projects\PhoneIndex\PhoneIndex\Program.cs:line 19
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
    
    
    Attached Images Attached Images
    Last edited by Falcon Eyes; May 13th, 2009 at 07:56 AM. Reason: Enhance my post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured