Search code examples
c#asp.netrdlcdynamic-rdlc-generation

How to change the Text of a Textbox in RDLC Reports based on Condition?


I have an RDLCReport. There is one TextBox named TextBox2 in that report that display a text like All Employees Record. Now i want to change the text of that textbox on the basis of some condition. Like when i click on Hired Employees button then the text of the TextBox2 should changed to 'Hired Employee Record' instead of All Employees Record and when i click on Rejected Employees button, then the text of the TextBox2 should changed to 'Rejected Employee Record'. Here is the conditions on a load event of the report page i am sending

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (EmployeeID.PrintAllEmployees == "ALL EMPLOYEES")
            {
                PrintAllEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "HIRED EMPLOYEES")
            {
                PrintHiredEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "REJECTED EMPLOYEES")
            {
                PrintRejectedEmployeesMethod();
            }
            else if (EmployeeID.PrintAllEmployees == "UNVERIFIED EMPLOYEES")
            {
                PrintUnverifiedEmployeesMethod();
            }
            else
            {
                //SOMETHING
            }
        }
    }

Here is the picture enter image description here

When the second condition returns true then the texbox text changed to Hired Employees Record and so on ....

My Second question is that, In the report, only the first page had Header Text, the remaining pages shows no heading text. How to achieve that? Please help me.


Solution

  • I asked a question here Couple of days before but i haven't got the answer. So I keep Searching and solved my problem. So i want to put an answer for that in hoping that if somebody got stuck here then someone get help from my answer. After all we have to help each other. So step by step.

    1) I add a Parameter from Report Data toolbox by right click and add parameter and named it paramHeader. enter image description here

    enter image description here

    enter image description here 2) I add a Textbox in Report .rdlc Design, and Drag and Drop paramHeader into Textbox.

    3) Then i add the following C# Code in my PrintReport method.

    ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptAllEmployeeRecord.rdlc");
            //Passing Parameter
            ReportParameterCollection reportParameters = new ReportParameterCollection();
            reportParameters.Add(new ReportParameter("paramHeader", "HIRED EMPLOYEES REPORT"));
            this.ReportViewer1.LocalReport.SetParameters(reportParameters);
    

    Here the ParamHeader is the parameter name that i added in the first step and HIRED EMPLOYEES REPORT is the string Value that i want to show in the TextBox that i added in the second step.

    So my overall Method will look like this

    public void PrintHiredEmployeesMethod()
        {
    
            //set Processing Mode of Report as Local  
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            //set path of the Local report  
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptAllEmployeeRecord.rdlc");
            //Passing Parameter
            ReportParameterCollection reportParameters = new ReportParameterCollection();
            reportParameters.Add(new ReportParameter("paramHeader", "HIRED EMPLOYEES REPORT"));
            this.ReportViewer1.LocalReport.SetParameters(reportParameters);
            //creating object of DataSet dsEmployee and filling the DataSet using SQLDataAdapter  
            DataSetAllEmployee dsemp = new DataSetAllEmployee();
            using (SqlConnection con = new SqlConnection(Base.GetConnection))
            {
                SqlCommand cmd = new SqlCommand(@"SELECT * FROM TableEmployee WHERE Status=@Status", con);
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@Status","HIRED");
                con.Open();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                adapt.Fill(dsemp, "dtAllEmployeeRecord");
            }
            //Providing DataSource for the Report  
            ReportDataSource rds = new ReportDataSource("DataSetAllEmployee", dsemp.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            //Add ReportDataSource  
            ReportViewer1.LocalReport.DataSources.Add(rds);
        }
    

    and my Page_Load Event will look like this

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (EmployeeID.PrintAllEmployees == "ALL EMPLOYEES")
                {
                    PrintAllEmployeesMethod();
                }
                else if (EmployeeID.PrintAllEmployees == "HIRED EMPLOYEES")
                {
                    PrintHiredEmployeesMethod();
                }
                else if (EmployeeID.PrintAllEmployees == "REJECTED EMPLOYEES")
                {
                    PrintRejectedEmployeesMethod();
                }
                else if (EmployeeID.PrintAllEmployees == "UNVERIFIED EMPLOYEES")
                {
                    PrintUnverifiedEmployeesMethod();
                }
                else
                {
                    //SOMETHING
                }
            }
        }
    

    NOTE that i have four different methods, but i am changing the Header Section according to my need. Like when a user want to print HIRED EMPLOYEES REPORT, then Header Section show HIRED EMPLOYEES REPORT, if a user want to generate ALL EMPLOYEES REPORT, then the Header Section should shows ALL EMPLOYEES REPORT and so on ..