Thursday, May 31, 2012

DataView.RowFilter() Vs DataTable.Rows.Find() Performance Test - Filter on Primary Key Field


In this post DataView.RowFilter() Vs DataTable.Rows.Find() Performance Test  - Filter on Primary Key Field  we, shall compare the performance of DataView.RowFilter () and DataTable.Rows.Find(), in performing a filter based on a Primary Key Field of the table.

DataTable.Select() Vs DataTable.Rows.Find() Performance Test - Filter on Primary Key


In this post DataTable.Select() Vs DataTable.Rows.Find() Performance Test - Filter on Primary Key we, shall compare the performance of DataTable.Select() and DataTable.Rows.Find(), in performing a filter based on the Primary Key of the table.

DataTable.Select() Vs DataView.RowFilter() Performance Test – Filtering Rows


In this post DataTable.Select() Vs DataView.RowFilter() Performance Test – Filtering Rows we, shall compare the performance of DataTable.Select() and DataView.RowFilter() in populating a GridView.

Wednesday, May 30, 2012

DataSet Vs DataReader Performance Test – Loading a GridView


In this post DataSet Vs DataReader Performance Test – Loading a GridView we, shall compare the performance of DataSet and DataReader in populating a GridView.

Asp.Net provides a number of ways to fill a GridView, like DataReader, DataSet etc, each one has its own advantages and disadvantages, we know that the DataReader is best suited for forward only read operations and the DataSet is best suited for offline data updates, we shall now see the difference in the loading time between the two in action.

The configuration of the system used to perform the evaluation is as follows.

OS
Windows XP Professional 2002 SP3
Processor
Pentium® D 2.66 GHz
RAM
3 GB




























The performance test was carried out for 25 iterations with both DataSet and DataReader; the results of the test are as follows.

Loading 1000 Records
DataSet Average time:             61.24 Milliseconds
DataReader Average time:       65.68 Milliseconds




Loading 2000 Records
DataSet Average time:             166.32 Milliseconds
DataReader Average time:       155.04 Milliseconds

















Loading 3000 Records
DataSet Average time:             229.04 Milliseconds
DataReader Average time:       182.34 Milliseconds
















Inference
Initially for 1000 rows, it appeared that the DataSet was performing marginally better than the DataReader, but as the number of rows increased to 2000 & later to 3000, the DataReader performed considerably better than the DataSet, thus proving the concept that the DataReader is best suited for ReadOnly forward only data.














The code used to perform the test is as follows

DataSet
SqlConnection objConn;
SqlCommand objCmd;
SqlDataAdapter objDA;
DataSet dsEmployee;
Stopwatch timer;
string strQuery = string.Empty;

timer = new Stopwatch();
timer.Start();

string strConn = ConfigurationManager.ConnectionStrings["EmployeesConnectionString"].ToString();
objConn = new SqlConnection(strConn);
strQuery = "SELECT * FROM Employee";
objCmd = new SqlCommand(strQuery, objConn);

objDA = new SqlDataAdapter(objCmd);
dsEmployee = new DataSet();
objDA.Fill(dsEmployee, "dtEmployee");

grdEmployees.DataSource = dsEmployee.Tables["dtEmployee"].DefaultView;
grdEmployees.DataBind();

timer.Stop();

lblExecutionTime.Text = "Execution Time (Milliseconds):" + timer.ElapsedMilliseconds.ToString();

DataReader
SqlConnection objConn;
SqlCommand objCmd;
SqlDataReader objDR;
Stopwatch timer;
string strQuery = string.Empty;

timer = new Stopwatch();
timer.Start();

string strConn = ConfigurationManager.ConnectionStrings["EmployeesConnectionString"].ToString();
objConn = new SqlConnection(strConn);
strQuery = "SELECT * FROM Employee";
objCmd = new SqlCommand(strQuery, objConn);
objConn.Open();
objDR = objCmd.ExecuteReader();
grdEmployees.DataSource = objDR;
grdEmployees.DataBind();
objConn.Close();
timer.Stop();

lblExecutionTime.Text = "Execution Time (Milliseconds):" + timer.ElapsedMilliseconds.ToString();

That’s it we have seen the performance difference between the DataSet and the DataReader in loading a GridView with data.

Tuesday, May 29, 2012

Asp.Net designer file not updating (VS.Net 2008)

Problem
In Visual Studio 2008, i came across a strange error (The name 'xxxxx' does not exist in the current context), any additions/changes done to the control names in the .aspx/.ascx files were not getting updated to the designer.cs file as a result the following error was thrown while building the project

The name 'xxxxx' does not exist in the current context
D:\\Source Code\Programming\ucSysInfo.ascx.cs 226 53

This was because there was a mismatch between the names of the controls name in the .ascx and the .ascx.cs.designer files, ideally the designer file should get updated once the name is changed in the .aspx/.ascx files, but at times this is not happening leading to the issue. 

Solution
The following workaround helped me overcome this issue

1. Close the .aspx/ascx files and the .cs files, if it is opened in the Editor
2. Delete the designer file corresponding to the .aspx/.ascx file (in my case it was ucSysInfo.ascx.cs.designer)
3. Right click on the .aspx/.ascx file in the Solution Explorer
4. Select the option Convert to Web Application.
5. That's it the designer file will now get recreated with the new/updated control names.

The Project should now BUILD without any errors.




LINQ to SQL Vs ADO.Net Performance Test – Calling Stored Procedures with OUT Parameter


In this post LINQ to SQL Vs ADO.Net Performance Test – Calling Stored Procedures with OUT Parameter we, shall compare the performance of LINQ to SQL and ADO.Net in executing a stored procedure call. This stored procedure takes the Department ID (int) as the input parameter and returns the Department Name (varchar(50)) as an OUT parameter

LINQ to SQL Vs ADO.Net Performance Test – Calling Stored Procedures


In this post LINQ to SQL Vs ADO.Net Performance Test – Calling Stored Procedures we, shall compare the performance of LINQ to SQL and ADO.Net in executing a stored procedure call. This stored procedure returns 1000 rows; the result set returned by the procedure is bound to a GridView control.

Monday, May 28, 2012

LINQ to SQL Vs ADO.Net Performance Test - DELETE


Asp.Net provides a number of Data Access Technologies, like ADO.Net, LINQ-to-SQL, Entity Framework etc, each one of these technologies has its own advantages and disadvantages, while designing an application; we need to identify the appropriate data access technology to achieve maximum efficiency.

LINQ to SQL Vs ADO.Net Performance Test - UPDATE


Asp.Net provides a number of Data Access Technologies, like ADO.Net, LINQ-to-SQL, Entity Framework etc, each one of these technologies has its own advantages and disadvantages, while designing an application; we need to identify the appropriate data access technology to achieve maximum efficiency.


LINQ to SQL Vs ADO.Net Performance Test - INSERT


Asp.Net provides a number of Data Access Technologies, like ADO.Net, LINQ-to-SQL, Entity Framework etc, each one of these technologies has its own advantages and disadvantages, while designing an application; we need to identify the appropriate data access technology to achieve maximum efficiency.

Friday, May 25, 2012

Asp.Net Performance Tuning


While designing larger and complex web sites, performance tuning is one of the key areas of consideration. Ignoring performance issues earlier will lead to large problems later when the page takes more time to load or when the load on the page (Number of users accessing the page simultaneously) is large.

Performance testing can be done with a set of tools which are available in the market, in fact there is a free tool Web Capacity Analysis Tool (WCAT) which can be integrated with IIS to test the load on the web server, but using these tools require a lot of time to be invested in configuring these tools, a few of the tools also cost a few bucks.

We shall see on how to measure the performance of a function or a code block using a couple of built in classes in .Net

Environment Class
The Environment Class helps us to measure the time elapsed to execute a code in Ticks.

Usage
int startTime, endTime;
startTime = Environment.TickCount;
    //Code to be Evaluated Goes Here.
endTime = Environment.TickCount;
endTime - startTime – Gives the time taken to execute the code in Milliseconds.

To know more on how to use this class refers the post Measure Execution time using Environment class


Stopwatch Class
The Stopwatch Class helps us to measure the time elapsed to execute a code in Milliseconds.

Usage
Stopwatch
 timer = new Stopwatch();
timer.Start();
    
//Code to be Evaluated Goes Here.
timer.Stop();
timer.ElapsedMilliseconds.ToString()
 – Gives the time taken to execute the code in Milliseconds.

To know more on how to use this class refers the post Measure Execution time using Stopwatch class

That’s it we have seen how the 2 simple classes Environment and Stopwatch to measure the performance of .Net code

LINQ to SQL Vs ADO.Net Performance Test - Loading a GridView


Asp.Net provides a number of Data Access Technologies, like ADO.Net, LINQ-to-SQL, Entity Framework etc, each one of these technologies has its own advantages and disadvantages, while designing an application; we need to identify the appropriate data access technology to achieve maximum efficiency.

LINQ to SQL Vs ADO.Net Performance Test - Loading a DropDownList






Asp.Net provides a number of Data Access Technologies, like ADO.Net, LINQ-to-SQL, Entity Framework etc, each one of these technologies has its own advantages and disadvantages, while designing an application; we need to identify the appropriate data access technology to achieve maximum efficiency.

Measure Execution time using Environment class


Measure Execution time using Environment class

Many a times we will have situations where we need to evaluate the performance of a function or a piece of code, Performance in the database can be measured using tools like SQL Profiler and other tools, but when it comes to the .Net code, we need to evaluate the time elapsed to execute the code.

Measure Execution time using Stopwatch class


Measure Execution time using Stopwatch class

Many a times we will have situations where we need to evaluate the performance of a function or a piece of code, Performance in the database can be measured using tools like SQL Profiler and other tools, but when it comes to the .Net code, we need to evaluate the time elapsed to execute the code.

LINQ to SQL - call stored procedure with OUTPUT parameters


LINQ to SQL - call stored procedure with OUTPUT parameters

Before writing a LINQ to SQL query; make sure that the DataContext is created, to know more on how to create the DataContext refer to the post LINQ to SQL Sample

Once the DataContext is in place we can go ahead and use it to process data from the mapped database.

Assume that we have the following StoredProcedure [
GetDepartmentName], in the database, which takes the DepartmentID as input and gives the DepartmentName as the output.

CREATE PROCEDURE [dbo].[GetDepartmentName]
      @DepartmentID int,
      @DepartmentName varchar(50) output
AS
BEGIN
      SELECT @DepartmentName = Name From Department 
      WHERE ID = @DepartmentID
END

Before using LINQ-to-SQL to call the StoredProcedure we need to add the StoredProcedure to the right side panel of the DataContext (.dbml) file.

Open the server Explorer and drag the procedure to the right side panel of the .dbml file, your panel should look as follows.



Once this is done we are ready to call the StoredProcedure from the code, the code is as follows.

EmployeeClassesDataContext dbContext = new EmployeeClassesDataContext();
string strDepartmentName = string.Empty;
dbContext.GetDepartmentName(1, ref strDepartmentName);
Response.Write("Department Name: " + strDepartmentName);

That’s it, we have called a Stored procedure with an OUTPUT parameter using LINQ-to-SQL.