nelek
nelek

Reputation: 4312

Better way for populating data from ms sql database in asp.net (vb.net/c#)

What is better way for populating data from database (table approx. 500 rows with min.5 cols): - using GridView (and I'll get huge viewstate value in header)?, or - using Repeater with Literal controls with enableviewstate="false" (but it must be populated after every postback, using tr with onclick attribute and hidden field, JavaScript)?

Is better way with large viewstate or reading mdf after every postback?

Thx.

Upvotes: 0

Views: 312

Answers (3)

maartendekker
maartendekker

Reputation: 462

It depends on where your concern lies.

If ease of handling events is your main concern, the gridview with viewstate enabled is your guy. It has great hook-ins for all kinds of actions the user may take. It does, however, come with the viewstate bandwidth cost you mentioned. But if you're audience uses relatively good connections and you need to get up and running fast, this can be a good alternative.

If bandwidth is your main concern, the repeater/literal approach will definitely save you some, but you'll have to write all your event logic yourself. The running of the same query for each postback is really redundant, but would in reality probably not take up that much perf, because of the db's caching mechanisms. However, you would still be sending back the complete table after each postback. The best bandwidth saving scenario, in my opinion, would be sending the complete table only once on load, and after that handle all table events with web service calls (you can use ASP.NET's Web-API framework for that). That way, only any actual changes would be sent over the wire.

Upvotes: 1

Ananthan Unni
Ananthan Unni

Reputation: 1304

If the data you expect to be rendered is heavy, why don't you implement any client side implementation? Like jqGrid or using KnockoutJS API? Then you can build a web service as the backend data supplier.

You can start with jqGrid here and KnockoutJS here

If you implement this using client side libraries, the application will perform better since there is no need of a heavy viewstate to persist the values and the server will not need to render the markup everytime.

Upvotes: 0

Racil Hilan
Racil Hilan

Reputation: 25351

You can also disable the viewstate on the GridView and that will make it faster. The Repeater is still faster than the GridView, but the difference shouldn't be big if you code it right. Also you can consider paging the GridView (i.e. displaying it in pages, let's say 10 rows at a time).

Reading from the database is usually faster and more efficient than sending large ViewState back and forth, but it's not only the speed that determines which way to go. There is security and other matters to consider. For example, getting the data from the database is much better for security than trusting the ViewState which can be tempered with easily.

Upvotes: 0

Related Questions