I’ve just started a new job and inherited a nightmare WebForms .net 4.0 project that’s going wrong in lots of places.
I’ve got a user control that is storing a DataTable in ViewState. The code is referencing the columns throughout the code by index number which is making it totally unreadable. I know I can reference columns by name to make it more readable but I’d prefer to break it out into a List.
The previous developer is storing the list in ViewState because changes can’t be persisted back to the database, we read in data, sales people can make modifications to pricing and then the information is then pushed into XML format to generate a sales order form in PDF. So we need a mechanism for storing temporarily.
If I had the luxury of starting from scratch I’d pull the data down in JSON and do it all client side but I don’t have that luxury just yet.
-
It’s been a long, long time since I worked with DataTables and I’m
pretty sure it’s not good to be placing this in ViewState, am I
correct? What kind of load would it be placing on the ViewState;
we’re looking at 44 columns and normally around 25 rows. :s -
Secondly, if there a big difference between placing a List in
ViewState as opposed to a DataTable or are they both as bad as each
other – in which case I’m doing no harm from my current position to
switch out? -
Third question, updates to rows are automatically updated in the
ViewState DataTable, would that be the same with a List? -
Last one, more probing for advice. Where would the best place to
store this information (preferably the List) server side – would
Session or Cache be better?
You are correct. It’s a terrible idea to store the DataTable in ViewState.
Storing a List in ViewState is still bad and perhaps marginally better than storing a DataTable.
It’s not clear what you mean by “updates occur automatically on the DataTable.” Unless you retrieve the DataTable from ViewState and apply any updates programmatically, I doubt that this is the case. You’d have to do the same if you use a List.
Session is probably better but then you may run into scalability issues. Frankly, I would store it in Session but would opt for using one of the Out of Proc state providers. This way your serialized Datatable is not sent to the client on every request making your page size horrendously big while giving you the option to store this serialized data on different places, SQL server or ASP.NET State Server, for example.