0

I've written a custom grid view and I want to save grid DataSource in ViewState but I got this exception

Type '<>f__AnonymousType0`7[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' in Assembly 'ExtAspNet.Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Now, I want to know how can I keep the grid DataSource?

2 Answers 2

1

Anything that you attempt to put in the viewstate must be decorated with the [Serializable] attribute, but because you have an anonymous type, you can't do it.

But besides that, don't do what you are trying to do, it will increase your page size considerably and unnecessarily. If anything, put your data source in Session and rebind it on postback but don't put it on viewstate.

Note, though, that putting a huge amount of data in session is not scalable or a good practice either, you have to base your decission depending on the size of your data and how much time it takes to get the data from the backend store. Have you measured how expensive it is to get the data, could you use Cache instead of Session, for example?

3
  • Up to 2500 users work with my application and all of my reports used this grid control, So I would rather keep data in ViewState instead of server memory. Nov 6, 2011 at 18:19
  • With all more reason: use some sort of distributed cache such as memcached or appfabric, ViewState is simply not the way to do this.
    – Icarus
    Nov 6, 2011 at 18:50
  • @Mehdi Hadjar, I think you miss concept of ViewState, it will slow down client side and the bandwidth is very high just times the ViewState with your user count to get the idea, does it not making your server also slow? If you have limited memory why not you query from db each postback or if you want to dump to client side, why you not store it in javascript variable like JSON and manipulate the data without postback.
    – CallMeLaNN
    Mar 28, 2012 at 1:25
0

if you are binding your gridview datasource with a datatable you can do like this....

Declare the datatable as follows and everything will work as expected

    private string _theDataTable="theDataTable";
    private DataTable theDataTable
    {
            get
            {
                    if(ViewState[_theDataTable]==null)
                            return new DataTable();
                    return (DataTable)ViewState[_theDataTable];
            }
            set
            {
                    ViewState[_theDataTable] = value;
            }
    }

cheers!

1
  • Thaks, but I had not problem with DataTable Nov 6, 2011 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.