Monday, November 1, 2010

ASP.NET server side paging

Front End
private void RefreshItems()
        {
            int item_count = 0;
            grdList.DataSource = _itemInvoice.GetInvoiceByFilter(branch_code, txtfRefNo.Text, Convert.ToDateTime(txtfRefDate.Text), txtfRemarks.Text, out item_count, curr_pageNumber, page_size, "RefDate", "ASC");
            grdList.DataBind();

            total_page = (int)Math.Ceiling(Convert.ToDecimal(item_count / page_size));

            if (total_page == 0) total_page = 1;

            lbPage.Text = "Page " + curr_pageNumber.ToString() + " of " + total_page.ToString();
            lblItems.Text = "(" + item_count.ToString() + " items)";

        }
Database Access Layer
internal Invoice[] GetInvoiceByFilter(string branchcode, string refno, DateTime refdate, string remarks, out int pageCount, int pageNumber, int pageSize, string sortColumn, string sortOrder)
        {
            base.com.CommandText = "dbo.spInvoiceWithFilter";
            base.com.Parameters.AddWithValue("@branchcode", branchcode);
            base.com.Parameters.AddWithValue("@refno", refno);
            base.com.Parameters.AddWithValue("@refdate", refdate);
            base.com.Parameters.AddWithValue("@remarks", remarks);
            base.com.Parameters.AddWithValue("@pageNumber", pageNumber);
            base.com.Parameters.AddWithValue("@pageSize", pageSize);
            base.com.Parameters.AddWithValue("@sortColumn", sortColumn);
            base.com.Parameters.AddWithValue("@sortOrder", sortOrder);

            SqlParameter param_out = new SqlParameter("@pageCount", SqlDbType.Int, 4);
            param_out.Direction = ParameterDirection.Output;
            base.com.Parameters.Add(param_out);

            _reader = base.GetDataReader();


            List list = new List();
            while (_reader.Read())
            {
                ItemSalesInvoice _is = new ItemSalesInvoice();
                _is.refno = _reader.GetString(1);
                _is.refdate = _reader.GetDateTime(2);
                _is.remarks = _reader.GetString(3);
                _is.RecID = _reader.GetGuid(4);
                list.Add(_is);
            }
            _reader.Close();
            int.TryParse(param_out.Value.ToString(), out pageCount);



            return list.ToArray();
        }
MS SQL stored procedure

CREATE Procedure dbo.[spInvoiceWithFilter] 
(
 @branchcode varchar(2),
 @RefNo varchar(11),
 @RefDate datetime,
 @Remarks varchar(90),
 @pageCount int output,
 @pageNumber int,
 @pageSize int,
 @sortColumn nvarchar(50),
 @sortOrder nvarchar(4)
)
AS 
BEGIN
 
 SET NOCOUNT ON; 

 DECLARE @Sorting NVARCHAR(55),
  @CharBranchCode NVARCHAR(2),
  @CharRefNo NVARCHAR(11),
  @CharRemarks NVARCHAR(50),
  @CharRecordFrom NVARCHAR(18),
  @CharRecordTo NVARCHAR(18),
  @Query NVARCHAR(4000),
  @fullRefNo VARCHAR(20);

 SET @fullRefNo = @branchcode +'-'+ @RefNo;
 
 SELECT @Sorting = @sortColumn +' '+ @sortOrder,
     @CharBranchCode = CAST(@branchcode AS NVARCHAR(2)),
     @CharRefNo =@CharBranchCode +'-'+ CAST(@RefNo AS NVARCHAR(11)),
     @CharRemarks = CAST(@Remarks AS NVARCHAR(50)),
     @CharRecordFrom = CAST(((@pageSize * (@pageNumber-1)) + 1) AS NVARCHAR(18)),
     @CharRecordTo = CAST((@pageSize * @pageNumber) AS NVARCHAR(18));

 SET @pageCount = (SELECT COUNT(RecID)
      FROM Invoice 
      WHERE (BranchCode =@branchcode)
      AND (RefNo LIKE @fullRefNo +'%')
      AND (Remarks LIKE @Remarks +'%'));

 SET NOCOUNT ON;

 SET @Query = 'SELECT * FROM 
 (SELECT ROW_NUMBER() OVER (ORDER BY '+ @Sorting +') AS RowNum,
    Substring(RefNo,4,11) AS RefNo,
    RefDate,
    Remarks,
    RecID
   FROM Invoice
  WHERE (BranchCode='''+  @CharBranchCode +''')
  AND (RefNo LIKE '''+ replace(@CharRefNo,'''','''''')+ '%'')
  AND (Remarks LIKE '''+ replace(@CharRemarks,'''','''''')+ '%''))';

 EXEC(@Query + 'AS tbl WHERE (RowNum BETWEEN ' + @CharRecordFrom + ' AND ' + @CharRecordTo + ') OR ' + @CharRecordTo + ' = 0');
END


No comments:

Post a Comment