The DataGrid control is most flexible and robust data control offered b ASP.NET. It displays data from the database table, accommodates in-row editing and updating of displayed data, sorting of columns, and built-in paging.
The DataGrid built-in paging –> Storing the data source that is being displayed in the DataGrid. The data source must be retrieved in each post back, this obviously the least efficient approach.
The paging offered by the DataGrid control requires minimal coding. This code handles the page changing events, refreshes the data sources and binds the DataGrid to the DataSource again. The drawback is that it requires the entire data source be generated or retrieved in each postback. If data source contains 100 rows of data and the DataGrid will be displaying 10 rows at a time, the DataGrid will navigate through 10 pages of data. The paging requires the entire DataSource be present and CurrentPageIndex being set. If the CurrentPageIndex is set to 3, the DataGrid will display the rows 31 through 40 from the DataSource. Retrieving the complete DataSource incurs significant overhead in most cases.
Custom Paging provided by DataGrid gives the developer complete control over the paging behavior. Custom paging is enabled through the AllCustomPaging attribute being set to true. With Custom paging enabled, the entire data source is not required to be available to the DataGrid because developer controls the data that is being displayed. With this in mind, the most efficient method for implementing custom paging would be to only return the rows of data from the data source that are to be displayed on the currently displayed page. This can be accomplished best using a stored procedure.
Creating the Stored Procedure
A stored procedure is the best suited solution because it will be processed by SQL Server with only the results of the stored procedure being returned. Decreasing the amount of data passed back will significantly improve the performance of any data call. Additionally, the first time that a stored procedure is executed, SQL Server creates an execution plan for the stored procedure and caches it so that future calls to the stored procedure are much faster. The stored procedure created is shown below.
Create Procedure dbo.sqlj_GetClientsByPage
-- Declare parameters.
@CurrentPage As tinyint, @PageSize As tinyint, @TotalRecords As int OUTPUT
As
-- Turn off count return.
Set NoCount On
-- Declare variables.
Declare @FirstRec int
Declare @LastRec int
-- Initialize variables.
Set @FirstRec = (@CurrentPage - 1) * @PageSize
Set @LastRec = (@CurrentPage * @PageSize + 1)
-- Create a temp table to hold the current page of data
-- Add an ID column to count the records
Create Table #TempTable (ClientId int IDENTITY PRIMARY KEY, Name varchar(50), Balance smallmoney)
--Fill the temp table with the reminders
Insert Into #TempTable (Name, Balance)
Select Name As Name, Balance As Balance From Clients Order By Name
--Select one page of data based on the record numbers above
Select ClientId, Name, Balance From #TempTable Where ClientId > @FirstRec And ClientId < @LastRec --Return the total number of records available as an output parameter Select @TotalRecords = Count(*) From Clients The stored procedure above accepts a parameter that designates the currently displayed page in the DataGrid as well as the capacity of rows to display on each page. It returns the total number of rows that is found. A temporary table is created and populated with all client rows. A second query then pulls the correct range of rows out of the data in the temporary table. Using the Stored Procedure with a DataGrid Control
Wednesday, July 9, 2008
Creating a stored procedure for Custom Paging with the ASP.NET DataGrid Control
Posted by
Nagendra Prasad
at
7/09/2008 05:38:00 AM
0
comments
Labels: SQL Server
Friday, July 4, 2008
You can ask SQL Server questions and find answers from expers
Posted by
Nagendra Prasad
at
7/04/2008 05:27:00 AM
0
comments
Labels: SQL Server
Tuesday, April 22, 2008
SQL SERVER - 15 Best Practices for Better Database Performance
Read the below 14 best practices and let me know what is as per your opinion should be the 15 best practice
1. Store relevant and necessary information in the database instead of application structure or array.
2. Use normalized tables in the database. Small multiple tables are usually better than one large table.
3. If you use any enumerated field create look up for it in the database itself to maintain database integrity.
4. Keep primary key of lesser chars or integer. It is easier to process small width keys.
5. Store image paths or URLs in database instead of images. It has less overhead.
6. Use proper database types for the fields. If StartDate is database filed use datetime as datatypes instead of VARCHAR(20).
7. Specify column names instead of using * in SELECT statement.
8. Use LIKE clause properly. If you are looking for exact match use “=” instead.
9. Write SQL keyword in capital letters for readability purpose.
10. Using JOIN is better for performance then using sub queries or nested queries.
11. Use stored procedures. They are faster and help in maintainability as well security of the database.
12. Use comments for readability as well guidelines for next developer who comes to modify the same code. Proper documentation of application will also aid help too.
13. Proper indexing will improve the speed of operations in the database.
14. Make sure to test it any of the database programming as well administrative changes.
Posted by
Nagendra Prasad
at
4/22/2008 02:08:00 AM
0
comments
Labels: SQL Server
Thursday, January 24, 2008
What is the advantage of putting the SQL statements inside a procedure?
Speed, security, modularity, consistency and reduced network congestion
Posted by
Nagendra Prasad
at
1/24/2008 02:13:00 AM
0
comments
Labels: SQL Server
Thursday, December 20, 2007
Optimizing SQL Queries
** Use stored procedures instead of heavy-duty queries.
This can reduce network traffic as your client will send to the server only the stored procedure name
(perhaps with some parameters) instead of all the text from a large heavy-duty query. Stored procedures can be used to enhance security.
** Avoid recompile the stored procedures.
Stored procedures are recompiled using the WITH RECOMPILE clause in the CREATE PROCEDURE or the EXECUTE statement.
Running stored procedures with sp_recompile
The WITH RECOMPILE option prevents reusing the stored procedure execution plan, so SQL Server does not cache a plan for this procedure and the procedure is always recompiled at run time.
Recompilations cannot be helped. So don’t create stored procedures with recompile.
Specify KEEPFIXED PLAN (to avoid recompiles) Forces the query optimizer not to recompile until schema is changed.
** Include the SET NOCOUNT ON statement in your stored procedures to stop the message indicating the number of rows affected by a Transact-SQL statement.
This can reduce network traffic due to the fact that your client will not receive the message indicating the number of rows affected by a Transact-SQL statement.
** Call stored procedures using their fully qualified name.
The complete name of an object consists of four identifiers: the server name, database name, owner name, and object name. An object name that specifies all four parts is known as a fully qualified name.
Using fully qualified names eliminates any confusion about which stored procedure you want to run and can boost performance because SQL Server has a better chance to reuse the stored procedures execution plans if they were executed using fully qualified names.
** Don't use the prefix "sp_" in the stored procedure name if you need to create a stored procedure to run in a database other than the master database.
This special prefix is reserved for system stored procedures. Although using this prefix will not prevent a user defined stored procedure from working, what it can do is to slow down its execution ever so slightly.
The reason for this is that by default, any stored procedure executed by SQL Server that begins with the prefix "sp_", is first attempted to be resolved in the Master database. Since it is not there, time is wasted looking for the stored procedure.
If SQL Server cannot find the stored procedure in the Master database, then it next tries to resolve the stored procedure name as if the owner of the object is "dbo". Assuming the stored procedure is in the current database, it will then execute. To avoid this unnecessary delay, don't name any of your stored procedures with the prefix "sp_".
** Before you are done with your stored procedure code, review it for any unused code
That you may have forgotten to remove while you were making changes, and remove it. Unused code just adds unnecessary bloat to your stored procedures.
** If you need to delete all the rows in a table, doesn’t use DELETE to delete them all, as the DELETE statement is a logged operation and can take time. To perform the same task much faster, use the TRUNCATE TABLE instead, which is not a logged operation.
** Indexes should be considered on all columns that are frequently accessed by the WHERE, ORDER BY, GROUP BY, TOP, and DISTINCT clauses.Without an index, each of these operations will require a table scan of your table, potentially hurting performance.
** Try to avoid using temporary tables inside your stored procedures.
Using temporary tables inside stored procedures reduce the chance to reuse the execution plan.
** Use the sp_executesql stored procedure instead of temporary stored procedures.Temporary stored procedures do not support the reuse of execution plans. Use the sp_executesql system stored procedure instead of temporary stored procedures in order to have a better chance of reusing the execution plans.
** Use SQL Server Profiler
To determine which stored procedures have been recompiled too often.
Deadlocks etc..
Posted by
Nagendra Prasad
at
12/20/2007 03:12:00 AM
0
comments
Labels: SQL Server
Wednesday, December 19, 2007
How do you test your database?
This is a very generic question. I will be describing my generic database testing method as well as stored procedure testing methods.
Testing Databases:- Table Column data type and data value validation.
- Index implementation and performance improvement.
- Constraints and Rules should be validated for data integrity.
- Application field length and type should match the corresponding database field.
- Database objects like stored procedures, triggers, functions should be tested using different kind of input values and checking the expected output variables.
Testing Stored Procedures:
- Understand the requirements in terms of Business Logic.
- Check that code follows all the coding standards.
- Comparing the fields' requirements of application to the fields retrieved by a stored procedure. they should match.
- Repeatedly run stored procedures many times with different input parameters and compare the output with expected results.
- Pass invalid input parameters and see if a stored procedure has good error handling..
Posted by
Nagendra Prasad
at
12/19/2007 10:26:00 PM
0
comments
Labels: SQL Server
what are the five top responsibilities of DBA?
I rate following five tasks as the most important responsibilities of DBA.
1. Securing the database from physical as well as logical integrity damage.
2. Restore the database from backup as part of disaster management plan.
3. Optimize the queries performance by proper indexing and optimizing joins, where conditions, select clause etc.
4. Design the new schema and support legacy schema as well legacy database systems.
5. Help developers to be better at writing SQL related code.
Posted by
Nagendra Prasad
at
12/19/2007 03:42:00 AM
0
comments
Labels: SQL Server
