Click here to Skip to main content
Click here to Skip to main content

Generic grid search and data paging

, 25 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Generic grid search and data paging explained.

DataFinder_article

Introduction

This article describes the DataFinder (user control) which proved to be very useful to me.

Background

  • Problem, usually presents the following requirements:
    • Create a page for displaying a list of records from a SQL query.
    • Enable paging of the grid, where if you get 1000000 records, and all this information through the network (in case the database is not on the same server as the applications), then this information is loaded on the memory of the server application where it is filtered for the records that we really want to see (the current page). All this causes bad performance, but surely, many of us already have applications running this way.
    • Finally, or as they say "Adding the cherry on the cake", add a generic search feature to this grid.
  • Analysis. For the solution, we have to:
    • Create our SQL query in a Stored Procedure.
    • Add code to get data from the query.
    • Create a GridView control and customize the columns to receive the data.
    • Improve our Stored Procedure to add parameters using which we send page info for the data in the database.
    • Finally, we need to add more functionality to our Stored Procedure to receive parameters to send our search criteria.
  • Solution. Everything listed in the two previous points can be done, but would take considerable time and will become a repetitive task evolving according to the system. This is where the user control DataFinder proves helpful. This control simplifies all our work, and further enhances our performance (performes paging in the database).

Using the Code

You need to download the solution accompanying this article, which has the search code and an example of how to use it.

  • Open the solution, and the search engine is in the Controles folder. Images and CSS used by this control are in the CSS/Buscador folder (try to keep the structure of files as found in the example, so you won't have to change the style sheet and paths of the images for the User Control).
  • In the web.config, we have the CNN link chain that connects the database "AdventureWorks". You can download this database from: AdventureWorks. It is important to mention here that I connect to a SQL Server 2008 database.
  • Create your SQL query and store it in the folder "Consultas" in the same folder that has the "Ejemplo.sql" file (the file does not necessarily have the .sql extension) with the suggested format. The identifier "- $TART -" is the delimiter to indicate where the query begins, and what you have before you is a comment.
  • --------------------------------------------
    -- Name: Kevin Guisbert Segales
    -- Company: KDev
    -- Date: 13/01/2010
    -- Copyright 2010
    -- La Paz - Bolivia
    --------------------------------------------
    -- $TART --
    SELECT  e.EmployeeID,
            e.NationalIDNumber,
            e.Title,
            e.VacationHours
    FROM    HumanResources.Employee e
  • Create an ASPX page. In this example, we have the page "Default.aspx" where you have to add the following file header attribute:
  • EnableEventValidation="false"
  • In the Head section, add the reference to the stylesheet of the page:
  • <link href="CSS/Buscador/Buscador.css" rel="stylesheet" type="text/css" />
  • Drag the user control "Buscador.ascx" to your page.
  • In the code-behind, add the following properties:
  • Protected Sub Page_Load(ByVal sender As Object, _
                  ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            '- Inicializamos datos del buscador -
            Me.Buscador1.Consulta = "Ejemplo.sql"
            Me.Buscador1.Titulo = "Buscador Genérico"
            Me.Buscador1.OrderBy = "Title"
            Me.Buscador1.DataBind()
        End If
    End Sub

    Where:

    • Consulta is the name of the file containing the query and is placed in "Consultas" folder (required).
    • Titulo is the title to show in the control (optional).
    • OrderBy is the name of the field by which to order the query (required).
    • DataBind() is the method to proceed with the binding (required).
  • If you have followed the steps above, you can proceed to run your application and enjoy the control.

I hope this control will be useful. I will soon be enhancing this control.

Points of Interest

This application was tested using:

  • Windows XP SP3
  • Internet Explorer 6
  • Visual Studio 2008
  • SQL Server 2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

KevGUID
Software Developer (Senior) RoadSight
Bolivia Bolivia
No Biography provided

Comments and Discussions

 
GeneralNice control PinmemberBrian Shifrin27-Jan-10 13:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 25 Jan 2010
Article Copyright 2010 by KevGUID
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid