How-to Get Best Performance With Large Amount Of Data In aspx page? - asp.net-2.0

I want to display 1000 rows and 80 rows in a GridView. I am little bit worried about the Performance of the application. If you know how to make it better performance ,please tell hw to do that in detailed way.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.FillGrid()
End If
End Sub
'Filling The Grid
Private Sub FillGrid()
Try
Dim MyCompany As New Company
Dim mydsCompanyDSource As DataSet
mydsCompanyDSource = MyCompany.DisplayData()
gvCompany.DataSource = mydsCompanyDSource
gvCompany.DataBind()
Catch ex As Exception
CreatelogFile(ex)
End Try
End Sub
Paging of the gridview is true . if Large amount of data will come , any performance problems will come ?

1000 rows is going to be heavy, anything over a 100 is a good candidate for Paging - only return 10 rows, for example, at a time.

Related

Rendering Finished ASP.net, VB.NET Loading GIF

I have been looking hard for a solution and can not seem to find anything.
Basically I have 50,000 rows in a database that is loading up rendering on the screen but takes about 20 seconds to load.
I was wondering if you can find out when asp has finished rendering, something like this;
Protected Sub Page Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
imgLoading.Visible = True
lvBigPage.Visible = False
Dim DE As New DE()
lvBigPage.DataSource = DE.BigPage_Get()
lvBigPage.DataBind()
End Sub
Private Sub lvBigPage_RenderingComplete(sender As Object, e As ListViewItemEventArgs) Handles lvBigPage.RenderingComplete
imgLoading.Visible = false
lvBigPage.Visible = True
End Sub
Using this I will be able to make some sort of load ajax gif until the page is loaded.

Value of type <DataTable> can not be converted to <database.table name>

We are developing an ASP.Net web application with a SQL Server database and would like to populate an ASP.Net label control with the total number of students who are enrolled at the school whenever the home page is displayed.
We created the following strongly typed controls with the dataset designer:
DataTable: Students
DataSet: DataSetAllStudents
TableAdapter: StudentsTableAdapter
In the VB.Net code-behind file I used the following code to start the process of obtaining a total count of enrolled students.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim studentsAdapter As New DataSetAllStudentsTableAdapters.StudentsTableAdapter
Dim studentsTableRow As Knowledge_Academy.Students
studentsTableRow = studentsAdapter.GetData
End Sub
We get an error on this line of code:
studentsTableRow = studentsAdapter.GetData
This is the error:
Value of type 'Knowledge_Academy.DataSetAllStudents.StudentsDataTable' cannot be
converted to 'Knowledge_Academy.Students'.
GetData contains the query that will return the total number of enrolled students. We would also like to know how to get the value returned into this ASP.Net label control.
<asp:Label ID="LabelTotalNumberOfStudents" runat="server" Text="Label"></asp:Label>
First, ensure that you load the data only on the first load and not on every postback. Second, since GetData returns a DataTable with all rows you can use it's Rows.Count property:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim daStudents = New DataSetAllStudentsTableAdapters.StudentsTableAdapter()
Dim tblStudents = studentsAdapter.GetData()
LblStudentNumber.Text = String.Format("{0}", tblStudents.Rows.Count)
End If
End Sub
Of course it would be better to add a new query to the TableAdapter that returns a scalar value with the number of students. That would require much less resources.

BackgroundWorker – Waiting for it to finish or a timeout elapses

I have a web page which is made up of a form for data entry and a panel for displaying the results. This page is written in VB.Net and the website hosting it is in ASP.Net.
Normal usage of the page is as follows:
The user inputs the form with some data/filters
The user presses the button "Search"
A BackgroundWorker starts finding the solutions
The BackgroundWorker instance is stored in a static variable, as I don't care about multi-user scenarios, but I'm not tied to this choice and I can change this. Also, the search process is asynchronous, but I really don't need to display anything while it's in progess.
The BackgroundWorker stores the result in a SolutionStorage object.
My goal is the following.
When the BackgroundWorker ends, the solutions found must be shown on the page. However, if after a fixed amount of time (currently, three minutes) it's still running, I want to terminate it and display the solutions present in the SolutionStorage in that moment.
The code goes as follows.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsNothing(worker) Then
worker.CancelAsync()
End If
worker = New BackgroundWorker()
worker.WorkerReportsProgress = False
worker.WorkerSupportsCancellation = True
AddHandler worker.DoWork, AddressOf workerDo
AddHandler worker.RunWorkerCompleted, AddressOf workerComplete
End Sub
Protected Sub search(ByVal sender As Object, ByVal e As EventArgs) Handles submitButton.Click
worker.RunWorkerAsync()
Thread.Sleep(180 * 1000)
If worker.IsBusy Then
worker.CancelAsync()
Dim solutions = repository.getSolutions()
'' Display solutions
If (solutions.Count > 0) Then
SolutionsRepeater.DataBind()
End If
End If
End Sub
Protected Sub workerDo()
' Collect data from the form
' Build the SolutionStorage
' Start the search
End Sub
Protected Sub workerComplete(sender As Object, e As RunWorkerCompletedEventArgs)
'' Display solutions
If (solutions.Count > 0) Then
SolutionsRepeater.DataBind()
End If
End sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsNothing(worker) Then
worker.CancelAsync()
End If
worker = New BackgroundWorker()
worker.WorkerReportsProgress = False
worker.WorkerSupportsCancellation = True
AddHandler worker.DoWork, AddressOf workerDo
AddHandler worker.RunWorkerCompleted, AddressOf workerComplete
End Sub
Is this a correct way of doing things? Is this the better way of doing things?
Locking down an asp .net thread with a sleep call for 3 minutes is not a very good solution.
I would recommend you to build a background system that could do search in the background so your asp .net pages returns immediately. Then you could refresh your page every 10 seconds to
see if the search is still running, finish or failed.
Here is a little trick you might be able to use, but it has some limitations and warnings:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
Read the comments to see the warnings and limitations.
But since you don't care about multi user support, and I assume the number of visits to your page is very limited, I can't see a direct problem with your current solution. But as soon as you expand it, it might cause you some problems.

DataPager ceases to work when PageSize is set in code

I have a standard ASP.Net DataPager with a standard ListView (using a DataTable as a data source).
When I set PageSize="24" in the design code:
<asp:DataPager runat="server" ID="DataPager1" PagedControlID="ListView1" QueryStringField="page" PageSize="24" >
the paging works as advertised.
However, when I then change that in code, in the Page_Load, eg:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DataPager1.PageSize = 48
End Sub
the paging ceases to work completely, while the initial loaded data set is indeed 48 items.
I can't see anything in the code which would affect this, so I'm wondering if I'm missing something - should I be changing something else?
Regards
Moo
Protected Sub DataPager1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataPager1.Init
DataPager1.PageSize = 48
End Sub
I managed to get this working by setting the page size property in the init event for the datapager.
This sort of error I usually find is a data-binding issue... either binding when you shouldn't, or not re-binding when appropriate. Hard to tell from your little snippet of code.
I am unfamiliar with the DataPager object, but I suspect it must rebind the data when you set the PageSize. If so, then every time the page loads it is re-binding and you are losing events. Have you tried this?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
DataPager1.PageSize = 48
End If
End Sub

ASP.Net page methods execution time monitoring

could anybody advice me, are there any approaches to calculate the methods execution time in asp.net page?
ASP.Net Trace doesn't suit me since it ignores ajax partial postbacks though just they are the tight place.
I have the page event which for some unknown reason executes slowly. Since the logic and event execution chain is rather complicated, I still unable to merely determine the culprit in debugger.
I'd like to measure the execution time of each method throughout the page, but I still see no way but insert redundant code lines (like timeBegin = DateTime.Now.... timeEnd=DateTime.Now = timeBegin etc) in the beginning and ending of each method. It seems to be ugly approach, does anybody know how to do it automatically? Are there tools which measure the methods execution time available?
I am not 100% sure the below works in all cases. So far it does though. Note that Context.Session.Item("CURRENT_WEB_USER") is where I store the current user, and is therefore not readily available as part of the asp.net session variables.
The code hereunder is in replacement of Global.aspx that allows both full page timing and timing of developer code.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_AcquireRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If HttpContext.Current.Session Is Nothing Then Return
If TypeOf HttpContext.Current.CurrentHandler Is Page Then
If Not Context.Items.Contains("Request_Start_Time") Then
Dim MyPage As Page = HttpContext.Current.CurrentHandler
AddHandler MyPage.Init, AddressOf PageInitEncountered
AddHandler MyPage.Unload, AddressOf PageUnloadEncountered
Context.Items.Add("Request_Start_Time", DateTime.Now)
Dim User As String = Context.Session.Item("CURRENT_WEB_USER")
Context.Items.Add("Request_User", User )
End If
End If
End Sub
Sub Application_ReleaseRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If TypeOf Context.CurrentHandler Is Page Then
Dim Page As Page = Context.CurrentHandler
Dim StartDate As Date = Context.Items("Request_Start_Time") : If StartDate = Nothing Then Return
Dim EndDate As Date = Now
Dim StartProc As Date = Context.Items("Page_Init")
Dim EndProc As Date = Context.Items("Page_Unload")
Dim User As String = Context.Items("Request_User")
LogEntry(StartDate, EndDate, StartProc, EndProc, User, Context.Request.Url.ToString)
End If
End Sub
Public Sub PageInitEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Init", DateTime.Now)
End Sub
Public Sub PageUnloadEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Unload", DateTime.Now)
End Sub
Public Sub LogEntry(StartDate As Date, EndDate As Date, StartProc As Date, EndProc As Date, User As String, PageURL As String)
System.Diagnostics.Debug.WriteLine(" (" & LogEntry.RequestMs & " - " & LogEntry.DurationMs & ") (" & User & ") " & PageURL)
End Sub
This may not be what you are looking for, but RedGate has a product called Performance Profiler that does exactly that. They do have a 15 day trial so you can even download it and try it out on your current issue. IMHO, it's worth every penny.
You can use asp.net performance counters and other things also. But that will not measure a page speed but they will measure a over all application performance.

Resources