Wednesday, December 7, 2022

Grid view real paging with output parameter ASP.NET C# - TotalRowcount

 void Bind(string PostHeld, int currentPage)

        {

                   

            int pageSize = 20;

             int totalRowCount = 0;

            int startRowNumber = ((currentPage) * pageSize) + 1;

            DataTable dt = Repository.GetEmployeeListWithPaging(PostHeld,startRowNumber, pageSize,out totalRowCount);

            

            //dt.DefaultView.Sort = sort;

            DataTable dt2 = ApplyFiltersWithSorting(dt, sort, "");

            GridView1.VirtualItemCount = totalRowCount;

            GridView1.DataSource = dt2;



            GridView1.DataBind();

            int ip = 0;

            try

            {

                //ip = Convert.ToInt32(dt2.Rows.Count);

                ip = totalRowCount;

            }

            catch { }


            if (Session["AccessType"].ToString() == "All")

            {

                //lblBadge.Text = new DataAccess().Read("pendingApprovalUS").Tables[0].Rows[0][0].ToString();

                //lblCorrectionRequests.Text = new DataAccess().Read("CorrectionRequestCountUS").Tables[0].Rows[0][0].ToString(); ;

            }

            else if (Session["AccessType"].ToString() == "Approver")

            {

                //lblBadge.Text = new DataAccess().Read("pendingApprovalDS").Tables[0].Rows[0][0].ToString();

Rownumber in grid with paging ASp.Net C#

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID,MainID" OnRowDataBound="GridView1_RowDataBound" CssClass="table table-bordered table-responsive" AllowPaging="True" AllowCustomPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="20" FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" OnInit="GridView1_Init" OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="OnSelectedIndexChanged">

        <Columns>

            <asp:TemplateField>

                <ItemTemplate>

                    <%# (Container.DataItemIndex + 1) + (GridView1.PageIndex * 20)%>

                    <%--<asp:Label runat="server" ID="lblRownumber" Text='<%# Bind("Rownumber") %>'></asp:Label>--%>

                </ItemTemplate>


Note: This is for 20 record per page

Filtering in ASP.NET C# Select query in C# - Filtering in C#

 public DataTable ApplyFiltersWithSorting(DataTable dt, string SortOrder, string PresentDesignation)

        {

            string FMSID = txtSearchFMS.Text.ToString().PadLeft(6, '0');

            string Expression = "Name  like '%%' ", Exp = "";

            if (txtSearchFMS.Text.Length > 0)

                Exp += "and FMSID like '%" + FMSID + "%'";

            if (ddlInstitute.SelectedIndex > 0)

                Exp += "and InstituteId='" + ddlInstitute.SelectedValue + "'";

            if (ddlDisciplines.SelectedIndex > 0)

                Exp += "and Discipline='" + ddlDisciplines.SelectedItem.Text + "'";

            if (ddlAdjustedDiscipline.SelectedIndex > 0)

                Exp += "and AdjustedDiscipline='" + ddlAdjustedDiscipline.SelectedItem.Text + "'";

            if (ddlPresentDesignation.SelectedIndex > 0)

                Exp += "and PresentDesignation='" + ddlPresentDesignation.SelectedItem.Text + "'";

            else if (PresentDesignation.Length > 0)

                Exp += "and PresentDesignation like '" + PresentDesignation + "%'";

            if (ddlRosterCategory.SelectedIndex > 0)

                Exp += "and RosterCategory='" + ddlRosterCategory.SelectedItem.Text + "'";

            if (ddlSmd.SelectedIndex > 0)

                Exp += "and SMDID='" + ddlSmd.SelectedValue + "'";

            if (ddlWSP.SelectedIndex > 0)

                Exp += "and WSP='" + ddlWSP.SelectedValue.ToString() + "'";

            if (txtSearchByName.Text.Length > 0)

                Expression = "Name like '%" + txtSearchByName.Text + "%' " + Exp;

            if (ddlAIPR.SelectedIndex > 0)

            {

                if (ddlAIPR.SelectedValue.ToString() == "True")

                    Exp += "and Len(trim(AiprFile))>'" + 2 + "'";

                else

                    Exp += "and AiprFile is NULL";

            }

            if (ddlVigilance.SelectedIndex > 0)

            {

                if (ddlVigilance.SelectedValue == "4")

                    Exp += "and VigilanceStatusId is null";

                else

                    Exp += "and VigilanceStatusId='" + ddlVigilance.SelectedValue.ToString() + "'";

            }

            Expression += Exp;

            DataRow[] rows = dt.Select(Expression, SortOrder);

            DataTable dtnew = new DataTable();

            try

            {

                return rows.CopyToDataTable<DataRow>();

            }

            catch { return null; }

        }


//calling of above function

DataTable dt2 = ApplyFiltersWithSorting(dt, sort, "");

How to pass output parameter in C# from SQL server Stored procedure

 public static DataTable GetEmployeeListWithPaging(string PostHeld, int startIndex, int pageSize , out int totalCount)

        {

            SqlParameter parTotalCount = new SqlParameter("TotalCount", SqlDbType.Int);


            parTotalCount.Direction = ParameterDirection.Output;


            SqlParameter[] param = { new SqlParameter("postheld", PostHeld),

                new SqlParameter("StartIndex", startIndex),

                new SqlParameter("PageSize",pageSize) ,

              parTotalCount };

            //new DataAccess().Read("read_cadre_info_paging", param);

            //totalCount = Convert.ToInt32(parTotalCount.Value);

            DataTable dt = new DataAccess().Read("read_cadre_info_paging", param).Tables[0];

            totalCount = Convert.ToInt32(parTotalCount.Value);

            return dt;


        }

// calling of above function

 int totalRowCount = 0;

            int startRowNumber = ((currentPage) * pageSize) + 1;

            DataTable dt = Repository.GetEmployeeListWithPaging(PostHeld,startRowNumber, pageSize,out totalRowCount);


reference:-https://www.c-sharpcorner.com/blogs/example-on-how-to-use-output-parameter-in-stored-procedure-in-c-sharp

https://www.c-sharpcorner.com/UploadFile/rohatash/get-out-parameter-from-a-stored-procedure-in-Asp-Net/

https://www.codeproject.com/Questions/539269/outputplusparameterplusinplusstoredplusprocedurepl



No mapping exists from object type System.Data.SqlClient.SqlParameter to a known managed provider native type ASP.NET C#

 reference:- https://stackoverflow.com/questions/19212706/no-mapping-exists-from-object-type-system-web-ui-webcontrols-textbox-to-a-known