Tuesday, June 7, 2022

Paging with Grid ASP.NET C# with output parameter in Stored Procedure

C# Code

 private void GetCustomersPageWise(int pageIndex)

    {

        string constring = Luminious.Connection.Configuration.ConnectionString;

        using (SqlConnection con = new SqlConnection(constring))

        {

            using (SqlCommand cmd = new SqlCommand("USP_MST_BATCHVIEW_CA_APPROVED", con))

            {

                cmd.CommandType = CommandType.StoredProcedure;

                //New Code Add on 23042020

                cmd.Parameters.AddWithValue("@KiaState", Convert.ToString(Session["KiaState"]));

                cmd.Parameters.AddWithValue("@CA_ID", Convert.ToString(Session["CAId"]));               

                cmd.Parameters.AddWithValue("@PageIndex", pageIndex);

                cmd.Parameters.AddWithValue("@PageSize", 50);

                cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);

                cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;

                con.Open();

                IDataReader idr = cmd.ExecuteReader();

                grdBatchVerification.DataSource = idr;

                grdBatchVerification.DataBind();

                idr.Close();

                con.Close();

                int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);

                this.PopulatePager(recordCount, pageIndex);

            }

        }

    }



    private void PopulatePager(int recordCount, int currentPage)

    {

        double dblPageCount = (double)((decimal)recordCount / decimal.Parse("50"));

        int pageCount = (int)Math.Ceiling(dblPageCount);

        List<ListItem> pages = new List<ListItem>();

        if (pageCount > 0)

        {

            pages.Add(new ListItem("First", "1", currentPage > 1));

            for (int i = 1; i <= pageCount; i++)

            {

                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));

            }

            pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));

        }

        rptPager.DataSource = pages;

        rptPager.DataBind();

    }




    protected void grdBatchVerification_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        this.GetCustomersPageWise(1);

    }


----------------------------------

SQL Code Stored procedure 

USE [ESDM_NIELIT_NEW]

GO

/****** Object:  StoredProcedure [dbo].[USP_MST_BATCHVIEW_CA_APPROVED]    Script Date: 07-06-2022 12:59:17 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[USP_MST_BATCHVIEW_CA_APPROVED]   

-- New Code Add on 23042020

 @KiaState INT     

,@CA_ID INT    

,@PageIndex INT = 1                

,@PageSize INT = 10                

,@RecordCount INT OUTPUT            

AS             

BEGIN             

SET NOCOUNT ON;                

SELECT DISTINCT ROW_NUMBER() OVER                

(                

 ORDER BY [Batch_id] desc               

)AS RowNumber                

,A.[Training_Partner_id]                

,A.[State_id]           

,B.State_Name               

,A.[Batch_id]                

,A.[Batch_Code]                

,A.[createdDate]         

,A.[Course_id]               

,A.Max_student_allowed

INTO #Results                

FROM [MST_BATCH] A          

INNER JOIN  dbo.MST_STATE B ON A.State_id=B.State_id           

where A.RequestStatus=2 and A.Agency_id=@CA_ID 

-- New Code Add on 23042020

and A.State_id =CASE WHEN @KiaState=0 THEN A.State_id ELSE @KiaState END

  

--ORDER BY  A.Training_Partner_id,A.createdDate desc          

ORDER BY  Batch_id desc

SELECT @RecordCount = COUNT(*)                

FROM #Results                

SELECT   * FROM #Results                

WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1                

DROP TABLE #Results                




END

--------------------------

aspx page code

<asp:GridView ID="grdBatchVerification" runat="server" GridLines="None"

          CssClass="table table-bordered table-condensed table-hover table-responsive" AutoGenerateColumns="false" DataKeyNames="Batch_id" 

          EmptyDataText="Record not found" ShowHeaderWhenEmpty="true" Width="100%" 

            onpageindexchanged="grdBatchVerification_PageIndexChanged" 

            onpageindexchanging="grdBatchVerification_PageIndexChanging" OnRowCommand="grdBatchVerification_RowCommand">

      <Columns>

      <asp:TemplateField HeaderText="S.No.">

</asp:GridView >

No comments:

Post a Comment