Wednesday, March 28, 2012

ReportViewer Code Behind?

Is there anyway to apply code behind to a report viewer? For example I am creating a form that has 4 text boxes. Depending on the record being pulled from the database, I want to put an "X" in one of those four text boxes.

So, it would be ideal to write some kind of conditional statement that will do this for me.

Is this possible?

You can easily pass parameters to the reports using report viewer check this links it will be great help

http://www.gotreportviewer.com/
http://www.gotreportviewer.com/localmodeparameters/index.html
http://msdn2.microsoft.com/en-us/library/ms251685(VS.80).aspx

|||

Thanks for the reply. However, I don't know if this will help me? I found the second article interesting, but it talks about checkboxes. There is no checkbox option in the reportviewer toolbox (only Pointer, Textbox, Line, Table, Matrix, Rectangle, List, Image, Subreport, & Chart).

Anyway, the sample is kind of vague. I'm new to .NET so I'm having a difficult time following this. Let me try to simplfiy my question:

In my .rdlc reportviewer I have a text box named "bobText". There is nothing in the text box right now. From the Web page, I am clicking on a specific person or record in the database. Clicking on this link opens this form. Depending on what person I clicked on from the Web page will determine what text is put into "bobText". So, in my .aspx page, how do I populate the value of "bobText" with whatever I want?

|||

I believe I figured it out using these URLs. Thanks for your help.

|||

Could you show us how you did it? I am having the same problem.

Thanks

Will

|||

Sure.

In your RDLC design, put a textbox out there. For an example, let's say the name of the text box is "phoneNumber".

Choose "Report" from the Properties drop down. Look for "ReportParameters" and click the "..." button to open Report Parameters dialog box. Name your parameter (ie: phoneText). Check the "hidden" check box if this field is initially empty. Click "Ok"

Click back on the "phoneNumber" text box. In the properties, find "Value" and enter this: =Parameters!phoneText.Value

That's all for the RDLC file. Now open up your aspx code behind. I use C#, so I'd open my aspx.cs file.

Also, I used a dataset for this. My dataset name is DataSet1.

In the code behind, you will need to add the following lines:

using Microsoft.Reporting.WebForms;
using DataSet1TableAdapters;

Change "DataSet1" to whatever the name of your dataset is.

In your class before the Page_Load subroutine, you'll need to add this:

view_PERSONNEL_RPTSTableAdapter RptAdapter =newview_PERSONNEL_RPTSTableAdapter();
ReportParameter phoneParam =newReportParameter();

Now, "view_PERSONNEL_RPTS" is the name of the view that's being accessed in my dataset. You should change this accordingly.

In your Page_Load subroutine, here's what you'll need. Here I am getting the row of data from the database and checking for values. It may not be exactly how you're doing it but it should still help.

DataSet1.view_PERSONNEL_RPTSDataTable GetEmpDatatable =newDataSet1.view_PERSONNEL_RPTSDataTable();
GetEmpDatatable = RptAdapter.GetData_RosterForm(id); // id is a variable I pass from the Web site. GetData_RosterForm is from my TableAdapter

DataSet1.view_PERSONNEL_RPTSRow GetEmpRow;
GetEmpRow = GetEmpDatatable[0]; // This gets the row of data and stores it into GetEmpRow

phoneParam.Name = "phoneText"; // This references the parameter we set in the RDLC file.

switch (GetEmpRow.TYPE.ToString().ToUpper())
{


case"CUSTOMER SERVICE":
phoneParam.Values.Add("1-800-555-1212");
break;

case"TECH SUPPORT":
phoneParam.Values.Add("1-800-555-TECH");
break;

default:
phoneParam.Values.Add("1-800-555-BLAH");
break;
}

ReportParameter[] p = { phoneParam };

try
{
this.ReportViewer1.LocalReport.SetParameters(p);

}

catch (Exception err)
{

Response.Write(err);

}

That's it! You can add as many parameters you want. Just make sure to add any other parameters to theReportParameter[] p array.
Hope it helps.

Jeremy

No comments:

Post a Comment