Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want fetch a huge data on site (about 19000 record) and show that on datalist control.
my data list have a paging feature and on the first time i show only 6 record on datalist.
then he user can go to page 2 and 3 and ...
fetch all record to data table get more time.
Please help me in details what should i use in sql server.
C#
 private void FetchDataToDataList()
    {DataTable dt = new DataTable();
        if (Cache["DataTable-cach"] == null)
        {
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand("SELECT dbo.table_name.field_name, FROM dbo.table_name ", con);
            con.Open();
             dt = new DataTable("T");
             string startime = System.DateTime.Now.ToLongTimeString();
            dt.Load(cmd.ExecuteReader());
            string endtime = System.DateTime.Now.ToLongTimeString();
            Cache.Insert("DataTable-cach", dt, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
            con.Close();
        }
        else 
        {
            dt = ((DataTable)Cache["DataTable-cach"]);
        }

// pagedDS is data list control
        PagedDataSource pagedDS = new PagedDataSource();
        pagedDS.DataSource = dt.DefaultView;
        pagedDS.AllowPaging = true;
        pagedDS.PageSize = 6;
        pagedDS.CurrentPageIndex = CurrentPage;
        dlPaging.DataSource = pagedDS;
        dlPaging.DataBind();
        lblCurrentPage.Text =  pagedDS.PageCount.ToString() +" صفحه    " +  (CurrentPage + 1).ToString()+ " از " ;
        // Disable Prev or Next buttons if necessary
        cmdPrev.Enabled = !pagedDS.IsFirstPage;
        cmdNext.Enabled = !pagedDS.IsLastPage;
       
    }
Posted

Here is the implementation using Stored Procedure


CREATE PROCEDURE GetEmployeeDetails
(
@PageNumber int,
@PageSize int
)
AS
BEGIN
DECLARE @StartRowNum int
DECLARE @EndRowNum int

SET @PageSize = ISNULL(@PageSize, 20) --As by default we display 20 records per page
SET @StartRowNum = @PageSize * (@PageNumber - 1) + 1
SET @EndRowNum = @PageNumber * @PageSize

SELECT * FROM
(SELECT EmpID, ROW_NUMBER() OVER(ORDER BY EmpID) as RowNum FROM Employee) as TempTable
WHERE RowNum >= @StartRowNum AND RowNum <= @EndRowNum
END

EXEC GetEmployeeDetails 2, 20
This will display 20 record per page
 
Share this answer
 
Instead of Sql query, use a stored procedure and implement paging within the stored procedure using ROW_NUMBER() function.
 
Share this answer
 
Hi,

I think, you need to optimize the query.Because you are so sure that you are going to show only 6 records per page then you can do it in sql query itself right?
You can use the below query to do it
SQL
SELECT field_name FROM (SELECT ROW_NUMBER() OVER (ORDER BY field_name)as rowNo,* FROM dbo.table_name)as TotalRecords
 WHERE TotalRecords.rowNo BETWEEN @startingPageNo AND @endingPageNo

Here, for parameter @startingPageNo AND @endingPageNo you need to pass the value from C# which might be easier to do.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900