Windows Develop Bookmark and Share   
 index > Windows Forms General > Serialization exception
 

Serialization exception

I have com+ component which is used through proxy. So to make it faster I made following sigleton class.

Can't figure out what seems to be the problem since nothingwon't be reallyreturned in recursive CreateNodesOfParent method

Here is the Exception:

System.Exception: Virhe Asiakasryhmien muodostuksessa! ---> System.Runtime.Serialization.SerializationException: The type System.Data.DataRowView in Assembly System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is not marked as serializable.
at Vesku.BusinessRules.Asiakkaat.AsiakasRyhmaNodes.CreateNodesOfParent(Int32 iParent, TreeNode pNode)
at Vesku.BusinessRules.Asiakkaat.AsiakasRyhmaNodes.HaeData()

And BTW this is old 1.1 framework stuff

Code Snippet

using System;

using System.Threading;

using System.Windows.Forms;

using System.Data;

namespace Vesku.BusinessRules.Asiakkaat

{

/// <summary>

/// Summary description for AsiakasRyhmaNodes.

/// </summary>

[Serializable()]

public class AsiakasRyhmaNodes

{

private static Mutex mtx = new Mutex();

private static AsiakasRyhmaNodes instance;

private System.Windows.Forms.TreeNode pRootNode;

private DatasetAsiakasryhmat ds;

protected AsiakasRyhmaNodes()

{

//

// TODO: Add constructor logic here

//

HaeData();

}

public void Remove(TreeNode n)

{

mtx.WaitOne();

TreeNode node = EtsiRyhmä(((Asiakasryhmä)n.Tag).Id, null);

node.Parent.Nodes.Remove(node);

mtx.ReleaseMutex();

}

public static AsiakasRyhmaNodes Instance()

{

mtx.WaitOne();

if(instance == null)

{

instance = new AsiakasRyhmaNodes();

}

mtx.ReleaseMutex();

return instance;

}

public void ForceReload()

{

mtx.WaitOne();

instance = new AsiakasRyhmaNodes();

mtx.ReleaseMutex();

}

public void HaeData()

{

Asiakasryhmienkäsittely ark = null;

try

{

ark = new Asiakasryhmienkäsittely();

ds = new DatasetAsiakasryhmat();

ds.Merge(ark.HaeKaikkiAsiakasryhmat()); // Getting data from DB

pRootNode = new TreeNode("Asiakasryhmät");

CreateNodesOfParent(0, pRootNode);

}

catch(Exception ex)

{

throw new Exception("Virhe Asiakasryhmien muodostuksessa!", ex);

}

finally

{

if(ark != null)

ark.Dispose();

if(ds != null)

ds.Dispose();

}

}

public TreeNode RootNode()

{

mtx.WaitOne();

return (TreeNode)pRootNode.Clone();

mtx.ReleaseMutex();

}

public TreeNode EtsiRyhmä(NullableTypes.NullableInt32 id, TreeNode node)

{

if(id.IsNull)

return null;

System.Collections.ArrayList ar = new System.Collections.ArrayList();

TreeNode ret = null;

if(node==null)

ar.InsertRange(0, pRootNode.Nodes);

else

ar.InsertRange(0, node.Nodes);

foreach(TreeNode n in ar)

{

if((n.Tag!=null) &&(((Asiakasryhmä)n.Tag).Id.Value==id.Value))

{

ret = n;

break;

}

if(n.Tag!=null)

{

ret = EtsiRyhmä(id,n);

if(ret!=null)

break;

}

if((n.Tag==null)&&(n.Nodes.Count>1))

{

ret = EtsiRyhmä(id,n);

if(ret!=null)

break;

}

}

return ret;

}

public void LisääRyhmä(Asiakasryhmä a)

{

mtx.WaitOne();

TreeNode node = new TreeNode();

node.Text = a.Nimi;

if(a.Sopimus!=null)

node.Text = node.Text + " ("+ a.Sopimus.Nimi+")";

node.Tag = a;

TreeNode newParent = EtsiRyhmä(a.YlempiAsiakasryhmäId, null);

if(newParent == null)

newParent = pRootNode;

newParent.Nodes.Add(node);

mtx.ReleaseMutex();

}

public void PaivitaRyhma(Asiakasryhmä a)

{

mtx.WaitOne();

TreeNode node = EtsiRyhmä(a.Id, null);

node.Parent.Nodes.Remove(node);

node.Text = a.Nimi;

if(a.Sopimus!=null)

node.Text = node.Text + " ("+ a.Sopimus.Nimi+")";

node.Tag = a;

TreeNode newParent = EtsiRyhmä(a.YlempiAsiakasryhmäId, null);

if(newParent == null)

newParent = pRootNode;

newParent.Nodes.Add(node);

mtx.ReleaseMutex();

}

public void CreateNodesOfParent(int iParent,TreeNode pNode)

{

Asiakasryhmienkäsittely ark = null;

try

{

DataView dv = new DataView(ds.AsiakasRyhmat);

ark = new Asiakasryhmienkäsittely();

if(iParent != 0)

{

dv.RowFilter = "[aryYlempi_AsiakasryhmaID] = " + iParent;

}

else

{

dv.RowFilter = "[aryYlempi_AsiakasryhmaID] " + "is null";

}

dv.Sort = "aryNimi";

foreach(DataRowView Row in dv)

{

Asiakasryhmä a = ark.MuodostaAsiakasRyhmä(Row);

TreeNode node = new TreeNode();

node.Text = a.Nimi;

if(a.Sopimus!=null)

node.Text = node.Text + " ("+ a.Sopimus.Nimi+")";

node.Tag = a;

if(pNode == null)

{

pRootNode.Nodes.Add(node);

}

else

{

pNode.Nodes.Add(node);

}

CreateNodesOfParent(int.Parse(Row["aryAsiakasryhmaID"].ToString()),node);

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

if(ark != null)

ark.Dispose();

// if(ds != null)

// ds.Dispose();

}

}

}

}

Any Ideas?
vipasane  Friday, September 07, 2007 8:01 AM

OK this was typical OCBD (Own Code Blindness Disorder) case. if I could read exceptions & my own code

foreach(DataRowView Row in dv)

{

Asiakasryhmä a = ark.MuodostaAsiakasRyhmä(Row); // what is passed here

vipasane  Tuesday, September 11, 2007 2:04 PM

Mostly, you want to use a serializable class to store persistent data in a file, so you probably want to try to limitthe functionality of the class to storing data rather than doing anything with it.However, your class might still work if you make your TreeNode class serializable, too (which the error would indicate).

Serilizable classes don't like null values, eitherso watch for that.

An example can be found here: http://www.codeproject.com/csharp/objserial.asp

A.Russell  Friday, September 07, 2007 9:50 AM

Replaced all TreeNodes with following but still get the same exception howcome it's complaining about DataRowView is not serializable?

Code Snippet

using System;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.Windows.Forms;

namespace Vesku.BusinessRules.Asiakkaat

{

/// <summary>

/// Summary description for SerializableTreeNode.

/// </summary>

[Serializable()]

public class SerializableTreeNode :TreeNode,ISerializable

{

public SerializableTreeNode(): base()

{

//

// TODO: Add constructor logic here

//

}

public SerializableTreeNode(string dirName) : base(dirName)

{

}

//Deserialization constructor.

public SerializableTreeNode(SerializationInfo info, StreamingContext ctxt)

{

//Get the values from info and assign them to the appropriate properties

int j=0;

j++;

Object o;

for (int i = 1; i <=(info.MemberCount-j); i++)

{

o = info.GetValue (i.ToString(), typeof(object));

Nodes.Add (o as TreeNode );

}

}

//Serialization function.

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)

{

int i = 1;

foreach (object o in Nodes)

{

if (o == null) continue;

info.AddValue (i.ToString(), o);

i++;

}

}

}

}

vipasane  Tuesday, September 11, 2007 11:43 AM

OK this was typical OCBD (Own Code Blindness Disorder) case. if I could read exceptions & my own code

foreach(DataRowView Row in dv)

{

Asiakasryhmä a = ark.MuodostaAsiakasRyhmä(Row); // what is passed here

vipasane  Tuesday, September 11, 2007 2:04 PM

You can use google to search for other answers

Custom Search

More Threads

• process hiding
• being able to detect all form closed
• Including Child Forms
• Howto limit TextBox entered text by visible area?
• Read Data from Excel File and store it to Database
• Overwriting Files (Permissions)
• yet another datagrid question
• IViewObject + SetAdvise Not Working
• Black label and persistent wait cursor
• combobox not getting populated with data