Thursday, January 26, 2012

Passing Parameters to RDLC

To pass parameters to rdlc write down below code to the code behind

ReportDataSource rds = new ReportDataSource("UnitPerformanceDataSet_sp_RptUnitPerformance", ds.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();


ReportParameter p1 = new ReportParameter("Showdt", "Date : " + DateTime.Now.ToShortDateString());
ReportParameter p2 = new ReportParameter("FromDt", "From : Year  " + Session["FromYear"].ToString() + "      Week No. " + Session["FromWeek"].ToString());
ReportParameter p3 = new ReportParameter("ToDt", "To : Year  " + Session["ToYear"].ToString() + "  Week No. " + Session["ToWeek"].ToString());
ReportParameter p4 = new ReportParameter("Zone", "Zone : " + Session["Zone"].ToString());
ReportParameter p5 = new ReportParameter("Unit", "Unit Name : " + Session["Unit"].ToString());


this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[]{p1,p2,p3,p4,p5});


ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
Now add parameters to rdlc.Open RDLC --> Click Report Menu
    

In VS-2008 You ll get Report Parameters option from here.







 But in VS-10 u wont get it here.U have to open ReportData tab --> Select Parameter --> Click New

A window ll popup, here u add parameters one by one which is defined in code behind.
Now DragNDrop a textbox from ReportItems --> Right Click --> Click Expression
Write down this code snippet in blank area after "=" and click ok.

Now browse and see the results.

You can Download Sample Code from Here

41 comments:

  1. Nice article.
    But when I am passing startDate as a parameter, it gives me an error message:

    Error 1 The Value expression for the text box ‘Textbox27’ refers to a non-existing report parameter ‘startDate’.

    ReplyDelete
    Replies
    1. This Error means that you have forgot to add the "startDate" parameter in rdlc..
      Open rdlc-->In menu bar look for "Report" option-->click on report parameter -->a new window open-->look for the "Add" button-->As u click on "Add" Button in right hand sides proprties options enabled -->In "Name" and "Prompt" textboxes write the name of your parameter like in your case it will be "startDate"--->click ok

      Delete
  2. I am getting an error Showdt parameter is missing a value. where as i have given a value =Parameters!ShowDt.value.

    after i execute a program it gives fallowing exeception-

    Microsoft.Reporting.WebForms.LocalProcessingException was unhandled by user code
    Message="An error occurred during local report processing."
    Source="Microsoft.ReportViewer.WebForms"
    StackTrace:
    at Microsoft.Reporting.WebForms.LocalReport.SetParameters(IEnumerable`1 parameters)
    at frmProposalReports.BindReport1() in d:\Adwait .net\CRMAD2\Backup\CRMLogin\frmProposalReports.aspx.cs:line 107
    at frmProposalReports.Button1_Click(Object sender, EventArgs e) in d:\Adwait .net\CRMAD2\Backup\CRMLogin\frmProposalReports.aspx.cs:line 114
    at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    ReplyDelete
    Replies
    1. You forgot to define "Showdt" in code behind.

      Delete
  3. An attempt was made to set a report parameter 'Showdt' that is not defined in this report.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.ReportingServices.Diagnostics.Utilities.UnknownReportParameterException: An attempt was made to set a report parameter 'Showdt' that is not defined in this report.

    Source Error:


    Line 105:
    Line 106: ReportParameter p1 = new ReportParameter("Showdt", "Date : " + DateTime.Now.ToShortDateString());
    Line 107: this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1 });
    Line 108:
    Line 109: ReportViewer1.LocalReport.Refresh();

    ReplyDelete
  4. Thanks for nice article

    how to pass parameter in loop such as generating Serial No with respect to Rows

    ReplyDelete
  5. http://dotnetknowledgebox.blogspot.in/2012/02/frequently-used-functions-in-rdlc.html

    Follow d above link.It includes some useful functions for rdlc including RunningValue() method which shows serial no rowwise.

    ReplyDelete
  6. Hi Anukana, I followed your tutorial and this is what I am doing.

    private void button4_Click(object sender, EventArgs e)
    {
    ReportViewer reportViewer1 = new ReportViewer();
    reportViewer1.LocalReport.ReportPath = "AddressLabelReport.rdlc";
    reportViewer1.ProcessingMode = ProcessingMode.Local;
    reportViewer1.LocalReport.SetParameters(new ReportParameter[] { new ReportParameter ("myparam1", "my value1", false), new ReportParameter("myparam2", "my value2", false) });
    reportViewer1.RefreshReport();
    }

    The plan is to load the report after a button is clicked. But I am getting a "LocalProcessingException" when trying to set parameters in 2nd last line. Do you see anything wrong here, plz?? Thanks for the article.

    ReplyDelete
  7. Dear Sanjay,
    If u want to load the report on button click then make reportviewer visible false on by default and set visibility true on button click.
    And in above code u haven't declared report data source.

    ReplyDelete
  8. only one line we should do it..

    this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { new Microsoft.Reporting.WinForms.ReportParameter("Report_Parameter_0", allnetwttotal.ToString()), new Microsoft.Reporting.WinForms.ReportParameter("Report_Parameter_1", QMModule.tb_FromDate.ToString("dd/MM/yyyy")), new Microsoft.Reporting.WinForms.ReportParameter("Report_Parameter_2", QMModule.tb_ToDate.ToString("dd/MM/yyyy")) });

    ReplyDelete
    Replies
    1. it's nice and useful article.... thanks for uploading .

      Delete
  9. Is there any way to pass user control textbox value in rdlc report as a parameter... or as a text

    ReplyDelete
  10. Thanks for the article, please join Asp.net CSharp Facebook Group to help people...

    ReplyDelete
  11. hi anukana saha,
    i need ur skype id plz send me and add in your skype, i have a big problem in task, and i want to show you on skype, so i think you will be solve my problem thanks

    skype Id: zubair.dev.drc

    ReplyDelete
  12. Hello Zubair,
    Being available at skype is problem for me.So it ll be better if u discuss it through mail.

    ReplyDelete
  13. I need to pass a session variable value to a parameter. how can i do it with the GUI shown above. please reply. Thanks in Advance

    ReplyDelete
  14. Must read the article properly.I have used session variable.

    ReplyDelete
  15. Very Nice article anukana.. :)

    ReplyDelete
  16. Hi..
    I Getting below error while generating the report...have look on this plz, what is the wrong i have done.

    -----------------Error------------------------------
    Microsoft.Reporting.WebForms.LocalProcessingException was unhandled by user code
    Message=An error occurred during local report processing.
    Source=Microsoft.ReportViewer.WebForms
    StackTrace:
    at Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, CreateAndRegisterStream createStreamCallback, Warning[]& warnings)
    at Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
    at Microsoft.Reporting.WebForms.LocalReport.Render(String format, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
    at Microsoft.Reporting.WebForms.Report.Render(String format, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
    at report.btnExcelReport_Click(Object sender, EventArgs e) in e:\CP\JLM\JLM\report.aspx.cs:line 280
    at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
    at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException: Microsoft.ReportingServices.ReportProcessing.ReportProcessingException
    Message=One or more parameters required to run the report have not been specified.
    Source=Microsoft.ReportViewer.Common
    ExceptionLevelHelpLink=http://go.microsoft.com/fwlink/?LinkId=20476&EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&EvtID=rsParametersNotSpecified&ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&ProdVer=1.0
    SkipTopLevelMessage=false
    StackTrace:
    at Microsoft.ReportingServices.ReportProcessing.ReportProcessing.RenderReport(IRenderingExtension newRenderer, DateTime executionTimeStamp, ProcessingContext pc, RenderingContext rc, IChunkFactory cacheDataChunkFactory, IChunkFactory yukonCompiledDefinition, Boolean& dataCached)
    at Microsoft.ReportingServices.ReportProcessing.ReportProcessing.RenderReport(IRenderingExtension newRenderer, DateTime executionTimeStamp, ProcessingContext pc, RenderingContext rc, IChunkFactory yukonCompiledDefinition)
    at Microsoft.Reporting.LocalService.CreateSnapshotAndRender(CatalogItemContextBase itemContext, ReportProcessing repProc, IRenderingExtension renderer, ProcessingContext pc, RenderingContext rc, SubreportCallbackHandler subreportHandler, ParameterInfoCollection parameters, DatasourceCredentialsCollection credentials)
    at Microsoft.Reporting.LocalService.Render(CatalogItemContextBase itemContext, Boolean allowInternalRenderers, ParameterInfoCollection reportParameters, IEnumerable dataSources, DatasourceCredentialsCollection credentials, CreateAndRegisterStream createStreamCallback, ReportRuntimeSetup runtimeSetup)
    at Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, CreateAndRegisterStream createStreamCallback, Warning[]& warnings)
    InnerException:
    ----------------------------End---------------------------------

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. 1.) Please checked parameters name whatever you have passed is that correct in ReportFile(.rdlc)..

      2.) If above case is well then checked your parameters data, if may be come as NULL or Blank then In reports file(.rdlc) you have to allow those permeates As Blank And NULL..

      Delete
    3. Thanks Ramani..

      It is very helpful..

      There was issue in second point from my side.Now issue is resolved.

      Delete
    4. i assume ds is a DataSet, but this code shows IOOFR exception: cannot find table[0], how to populate it?

      Delete
  17. Oh what a gr8 post.. I like it and it helped me very much...

    ReplyDelete
  18. Hi Anu,
    I'm unable to see the image no: 5.
    So i'm getting confused.

    please verify

    Thanks.

    ReplyDelete
    Replies
    1. If image is not clearly visible on ur browser or what?

      Delete
    2. Image is not at all visible...
      I tried in different browsers (Mozilla, Chrome and IE)
      But it shows like image got broke(without any details).

      Delete
  19. This comment has been removed by the author.

    ReplyDelete
  20. hi anushka.

    how to print multiple columb in bar code stickre..........using windows form....like this....
    i want full code ......pls

    also no number of copies.....using button
    ..........................................................
    name .
    |||||||||||||||||||||||||||| .
    number price .
    again name add .
    ..............................................










    ReplyDelete
  21. hi anukana,
    I will be storing(upload reports) the rdlc definition as an XML content, i will intiliaze the report object by loading the RDLC definition(XML content), report getting generated properly but when i try to set parameter value getting an exception saying parameter "p1" is readonly. Do you have any idea about this?

    ReplyDelete
  22. hi anuska,

    I want to pass a image path in parameter to report viewer. I have used your code but while running the report viewer only showing "Loading". How to fix this ?

    ReplyDelete
  23. hi anuska,
    How to use a button and combobox for displaying rdlc report

    ReplyDelete
  24. hi, i cannot download this sample code. Please can send email to me or any dropbox/share drive to get it? Thanks

    ReplyDelete
  25. Hi, i can't generate Rdlc report dynamically using report viewer in Vb.How can i generate..Please help...Thanks

    ReplyDelete
  26. Can we pass Class object as report parameter, instead of int/string/bool/float ?

    ReplyDelete
  27. Can't Download Sample code

    ReplyDelete
  28. It is 'ReportViewer1.LocalReport.ReportPath' necessary in rdlc report ?

    ReplyDelete
  29. Hi,
    I am Working on Hybrid Application(MVC Controller Calling cshtml and moving to Web Form), that time i am unable to get existing Session that create two session, i applied your parameter code that is not coming to ASPX from CSHTML file
    Can you plz give me some hint on it...?

    Thanks in Advance

    ReplyDelete
  30. Error 45 The FilterValue expression for the tablix ‘Tablix1’ contains an error: [BC30451] 'session' is not declared. It may be inaccessible due to its protection level. D:\Sunny\WMS project\WebSite8 (2)\WebSite8\requester_report.rdlc 1

    ReplyDelete