ObjectDataSource and ViewState

  • Thread starter Giorgio Parmeggiani
  • Start date
G

Giorgio Parmeggiani

Hi

I have in a ASPX page a ObjectDataSource and a GridView
I have also a web control for filter the grid rows. The webcontrol have a
public property to define a filter. This property is a custom object not
serializable (i'm using NHibernate is a 'NHibernate.Expression.EqExpression'
).

I have defined in the ObjectDataSource a SelectParameters (<asp:ControlParameter
...... />) related at the public property of my filter webcontrols

When i use my webcontrol to filter the grid rows i obtain this exception:
System.Runtime.Serialization.SerializationException because the framework
try to save in the page's ViewState the property value which is not serializable!


How can i solve this problem? Can i avoid that the framework try to save
the property value in the viewState?

Thank in advance and excuse me me for my bad english language

Giorgi
 
W

Walter Wang [MSFT]

Hi Giorgio,

Thank you for your post!

Based on my understanding, the question is:
1) You have a WebControl which expose a public property whose return type
is NHibernate.Expression.EqExpression
2) You have an ObjectDataSource, whose SelectParameters contains the above
data type as a control parameter
3) You bind the ObjectDataSource to a GridView
4) You get an exception
"System.Runtime.Serialization.SerializationException"
If there's anything I have misunderstood, please feel free to post here.

The root cause of this problem is because the public property type of your
filter WebControl is a 3rd party class which doesn't support serialization.

To make a data type compatible to ViewState, we can use one of following
methods:
1) Mark the base type as [Serializable]
2) Associate a TypeConverter to the type

Although the first method is simplest, since you're using a 3rd party class
here, we cannot use it here.

As for the 2nd method, I think you can try it first to see if it works:
1) Inherit from NHibernate.Expression.EqExpression and associate it with
your custom TypeConverter, for example:

[TypeConverter(typeof(MyEqExpressionConverter))]
public class MyEqExpression : EqExpression
{
}

2) Create a custom TypeConverter for your class:

public class MyEqExpressionConverter : TypeConverter
{
}

Please refer to following documentation:

#How to: Implement a Type Converter
http://msdn2.microsoft.com/en-us/library/ayybcxe5.aspx

3) Modify your WebControl's public property type to 'MyEqExpression'

Since a MyEqExpression is-a EqExpression, normally this will not break your
existing functions which require EqExpression as parameter type.

If this approach doesn't work or you have anything unclear, please feel
free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Giorgio Parmeggiani

Hi Walter,

Thank you for your reply!
Based on my understanding, the question is:
1) You have a WebControl which expose a public property whose return
type
is NHibernate.Expression.EqExpression
2) You have an ObjectDataSource, whose SelectParameters contains the
above
data type as a control parameter
3) You bind the ObjectDataSource to a GridView
4) You get an exception
"System.Runtime.Serialization.SerializationException"
If there's anything I have misunderstood, please feel free to post
here.
The root cause of this problem is because the public property type of
your filter WebControl is a 3rd party class which doesn't support
serialization.

Yes! Is all ok!
To make a data type compatible to ViewState, we can use one of
following
methods:
1) Mark the base type as [Serializable]
2) Associate a TypeConverter to the type
Although the first method is simplest, since you're using a 3rd party
class here, we cannot use it here.

As for the 2nd method, I think you can try it first to see if it
works: 1) Inherit from NHibernate.Expression.EqExpression and
associate it with your custom TypeConverter, for example:

Thank for your suggestion, it seems very great.
I think that i will modify my webcontrol turning the public property into
a serializable custom type.
Subsequently the class that will concretely make the select will turn the
property value into a NHibernate.Expression type

This because the NHibernate.Expression type are too much (more than 20).

Thank you very much

Giorgio Parmeggiani
 
W

Walter Wang [MSFT]

Thank you for your update!

Please feel free to post here if there's anything I can help.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top