Question

I have a simple asp FormView on my page, it's working fine and everything, but I noticed the page is somewhat slow to load up (2-3 seconds), when there's really no reason for it, it's such a simple page.

The problem is that the FormView is using a SqlDataSource, which is using a 'heavy' select statement, basically grabbing all the fields and all rows of a profile table (name, address, phone, etc.). I'm assuming the FormView needs to get all the records so the Paging works correctly, but is there a way to code it or an attribute I can use so it doesn't grab every row and column but just all the columns of the current NameId?

asp:

<asp:FormView ... DataSourceId="sdsNames" AllowPaging="True" DataKeyNames="NameId">
   <EditItemTemplate>....</EditItemTemplate>
   <InsertItemTemplate>...</InsertItemTemplate>
   <ItemTemplate>
       <asp:Label Text='<%# Eval("Name") %>'.../> 
       ...
   </ItemTemplate>
 </asp:FormView>
 <SqlDataSource ID="sdsNames" ... SelectCommand="SELECT * from tblNames">

I tried changing the SelectCommand to

SELECT * from tblNames where NameId=1

since the initial page should show the first entry. But then I lose the pagination, basically no way to view the next record.

What the Page looks like:

enter image description here

Was it helpful?

Solution

There is not a lot you can do to improve performance here. You're correct, the SqlDataSource does slow things down because it retrieves all of the rows from your table.

There are a couple of different approaches that come to my mind:

Remove fields from your query.
This is the simplest solution, though it might not apply to your situation. If there are fields you don't need to display / modify, remove them. So, rather than SELECT * FROM..., you have SELECT LName, FName, Mname FROM...

You're still getting every row, but at least there is less data in each row.

You could switch to the ObjectDataSource control.
I haven't used this control much, but MSDN says that it performs a little better, because it doesn't always have to retrieve every row:

Some data sources, such as the ObjectDataSource control, offer more advanced paging capabilities. In these cases the FormView control takes advantage of the data source's more advanced capabilities to gain better performance and flexibility while paging. The number of rows requested may vary depending on whether the data source supports retrieving the total row count.

You could move away from the datasource controls.
This probably involves more complexity than what you're looking for, but it is the most flexible approach. You can retrieve just the columns and rows you want. You can create a PagerTemplate, and set the values when the FormView is databound. You would have to handle the PageIndexChanging and PageIndexChanged events to change the paging values yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top