locked
Report Parameters with ReportViewer? RRS feed

  • Question

  • User755602676 posted

    I have a couple of questions regarding parameters using ReportViewer.

    1. Parameters for sp 

    I have a ReportViewer that takes one parameter that it passes to a stored procedure to return a DataTable, but I do not know how to set the value of that parameter when I set ReportViewer.Visible = True.

    2. I'm looking for a way to pass parameters directly to a report (these parameters carry custom text to display in the report depending on choices made by users when requesting the report and have nothing to do with the parameter in question 1 that is needed for the sp).

     Thank you.

    Monday, May 5, 2008 7:03 PM

Answers

  • User1869667223 posted

    2. I'm looking for a way to pass parameters directly to a report (these parameters carry custom text to display in the report depending on choices made by users when requesting the report and have nothing to do with the parameter in question 1 that is needed for the sp).

    Yes, We Can do like this also.. be ensure you have added starttime and  endtime parameters added in Report File . For adding  parameters in report

    Menu Report--> Report Parameters.

     // To set the parameter values                               
                    ReportParameterInfoCollection parms = ReportViewer1.LocalReport.GetParameters();                       
                    foreach(ReportParameterInfo parm in parms)
                    {                   
                        SetReportParameters(parm.Name);
                    }  

    private void SetReportParameters(String parameterName)
            {
                try
                {
                   if(parameterName.ToLower() == "starttime")
                    {
                        ReportParameter startTimeParameter = new ReportParameter(parameterName, txtStartDate.Text);                   
                        this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { startTimeParameter });
                    }
                    else if(parameterName.ToLower() == "endtime")
                    {
                        ReportParameter endTimeParameter = new ReportParameter(parameterName, txtEndDate.Text);
                        this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { endTimeParameter });
                    }               
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    In Report File you add this in filter by  Table Properties -- > Data -- > Filter options  Or we can display the starttime and endtime parameters in header sections.

    For More detailed informations go thru this links

    http://www.gotreportviewer.com/  

    http://msdn.microsoft.com/en-us/library/aa337432.aspx

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 6, 2008 1:26 AM
  • User755602676 posted

    Well, this is embarassing: another typo. OK, here's the real correct code:

    ReportViewer1.ProcessingMode = ProcessingMode.Local
    ReportViewer1.ReportPath = "QM.rdlc"

    Dim prmQrTitle As New ReportParameter("Hdr_QrTitle", "Questionnaire 1")
    Dim prmProgram As New ReportParameter("Hdr_Program", "Evening")
    Dim parameters() As ReportParameter = {prmQrTitle, prmProgram}
    ReportViewer1.LocalReport.SetParameters(parameters)  <-- BLOWS UP HERE

    ReportViewer1.Visible = True
    ReportViewer1.Refresh()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 8, 2008 6:12 PM
  • User1869667223 posted

    Hi DesertDude,  

    How DO I set things up so that I can assign a report to a ReportViewer and a DataTable to the LocalReport in code?

    Dim rdsReport As New ReportDataSource
         With rdsReport
              .Name = "SalesReport"
              .Value = dtReport 'Created earlier in the code
         End With
         .LocalReport.DataSources.Clear()
         .LocalReport.DataSources.Add(rdsReport)

    The dataTable name you are passing not matching with rdlc dataTable name,

    A data source instance has not been supplied for the data source 'SalesReport_SalesReportTable'
     

    Change the name in ReportDataSource as "SalesReport_SalesReportTable"

    Dim rdsReport As New ReportDataSource
         With rdsReport
              .Name = "SalesReport_SalesReportTable"
              .Value = dtReport 'Created earlier in the code
         End With
         .LocalReport.DataSources.Clear()
         .LocalReport.DataSources.Add(rdsReport)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2008 1:35 AM

All replies

  • User1869667223 posted

    2. I'm looking for a way to pass parameters directly to a report (these parameters carry custom text to display in the report depending on choices made by users when requesting the report and have nothing to do with the parameter in question 1 that is needed for the sp).

    Yes, We Can do like this also.. be ensure you have added starttime and  endtime parameters added in Report File . For adding  parameters in report

    Menu Report--> Report Parameters.

     // To set the parameter values                               
                    ReportParameterInfoCollection parms = ReportViewer1.LocalReport.GetParameters();                       
                    foreach(ReportParameterInfo parm in parms)
                    {                   
                        SetReportParameters(parm.Name);
                    }  

    private void SetReportParameters(String parameterName)
            {
                try
                {
                   if(parameterName.ToLower() == "starttime")
                    {
                        ReportParameter startTimeParameter = new ReportParameter(parameterName, txtStartDate.Text);                   
                        this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { startTimeParameter });
                    }
                    else if(parameterName.ToLower() == "endtime")
                    {
                        ReportParameter endTimeParameter = new ReportParameter(parameterName, txtEndDate.Text);
                        this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { endTimeParameter });
                    }               
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    In Report File you add this in filter by  Table Properties -- > Data -- > Filter options  Or we can display the starttime and endtime parameters in header sections.

    For More detailed informations go thru this links

    http://www.gotreportviewer.com/  

    http://msdn.microsoft.com/en-us/library/aa337432.aspx

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 6, 2008 1:26 AM
  • User755602676 posted

    Thanks for your reply. I believe this is useful. I'll find out soon when I get a chance to try it and I'll let you know. The only other question remaining is how to set the parameter in the GetData method of the OjbectDataSource or TableAdapter, not sure which one has that method. But this is a parameter that is passed to the SP to return a datatable.

    Thanks, again.

    Wednesday, May 7, 2008 6:34 PM
  • User1869667223 posted

    Hi,

    Have look on this link .

    http://msdn.microsoft.com/en-us/library/57hkzhy5.aspx

     

    Thursday, May 8, 2008 12:54 AM
  • User755602676 posted

    I'm trying to use the code you listed earlier in this thread. I keep getting an error, "An error occurred during local report processing.", on the SetParameters line (see the code below).

    The visible property of the ReportViewer control is set to False in the VS2005 GUI. No other properties of the control have been set. Also, I have a report called "QM.rdlc" in my project with two parameters (defined in the Reports menu) called "Hdr_QrTitle" and "Hdr_Program". These parameters are not used for processing data; they are used simply for display within the report. I'm using VB.Net in VS2005. Here's the code (I'm not too sure about the last 2 lines; I never got to that point):

    ReportViewer1.ProcessingMode = ProcessingMode.Local
    ReportViewer1.ReportPath = "QM.rdlc"

    Dim prmQrTitle As New ReportParameter("Hdr_QuestionnaireTitle", "Questionnaire 1")
    Dim prmProgram As New ReportParameter("Hdr_Program", "Evening")
    Dim parameters() As ReportParameter = {prmQrTitle, prmProgram}
    LocalRpt.SetParameters(parameters)  <-- BLOWS UP HERE

    ReportViewer1.Visible = True
    ReportViewer1.Refresh()

     
    Any ideas? Thanks for your help!

    Thursday, May 8, 2008 6:06 PM
  • User755602676 posted

    Sorry, there's a typo in the code I sent you (name of the parameter is wrong). Here's the correct code:

    ReportViewer1.ProcessingMode = ProcessingMode.Local
    ReportViewer1.ReportPath = "QM.rdlc"

    Dim prmQrTitle As New ReportParameter("Hdr_QrTitle", "Questionnaire 1")
    Dim prmProgram As New ReportParameter("Hdr_Program", "Evening")
    Dim parameters() As ReportParameter = {prmQrTitle, prmProgram}
    LocalRpt.SetParameters(parameters)  <-- BLOWS UP HERE

    ReportViewer1.Visible = True
    ReportViewer1.Refresh()

    Thursday, May 8, 2008 6:09 PM
  • User755602676 posted

    Well, this is embarassing: another typo. OK, here's the real correct code:

    ReportViewer1.ProcessingMode = ProcessingMode.Local
    ReportViewer1.ReportPath = "QM.rdlc"

    Dim prmQrTitle As New ReportParameter("Hdr_QrTitle", "Questionnaire 1")
    Dim prmProgram As New ReportParameter("Hdr_Program", "Evening")
    Dim parameters() As ReportParameter = {prmQrTitle, prmProgram}
    ReportViewer1.LocalReport.SetParameters(parameters)  <-- BLOWS UP HERE

    ReportViewer1.Visible = True
    ReportViewer1.Refresh()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 8, 2008 6:12 PM
  • User755602676 posted

    so, the problem was that I was giving it the wrong path to the report. The code you supplied was the answer. Thank you.

    Friday, May 9, 2008 5:23 PM
  • User755602676 posted

    Sasi, if you are still there, I wonder if you'd be willing to help me a bit further. I'm not getting errors anymore with the parameters, but I still can't get the report to render. I'm having trouble with datasources.

    My report (SalesReport.rdlc) has two string parameters, Quarter and AgencyType. Here's the code I'm using (I'm not using the Source Code tool in this editor as it keeps resulting in errors):

    With ReportViewer1
         .Visible = True
         .Reset()

         .LocalReport.ReportPath = "SalesReport.rdlc"

         Dim rdsReport As New ReportDataSource
         With rdsReport
              .Name = "SalesReport"
              .Value = dtReport 'Created earlier in the code
         End With
         .LocalReport.DataSources.Clear()
         .LocalReport.DataSources.Add(rdsReport)

         'All RptParam_ variables have been set earlier in the code
         Dim prmQrTitle As New ReportParameter("Quarter", RptParam_Qarter)
         Dim prmAgencyType As New ReportParameter("AgencyType", RptParam_AgencyType)
         Dim parameters() As ReportParameter = {prmQuarter, prmAgencyType}
         .LocalReport.SetParameters(parameters)

         .DataBind()
         .LocalReport.Refresh()

    End With

    Here are the problems I am experiencing:

    If I use the SmartTag of the ReportViewer to create a dataset, it ends up creating a TableAdapter and an ObjectDataSource. I then assign the resulting DataTable to the DataSetName property of the table in the report. When I run this, it goes through the code without an error, but where the report should be rendered (where the ReportViewer is), I get this error:

    A data source instance has not been supplied for the data source 'SalesReport_SalesReportTable'

    If I reset (delete the value of) the DataSetName property, I get the same error. If I then open the rdlc in a text error and delete the <DataSets> element and all its contents, I get this error at complile:

    Error 1 The table ‘table2’ is in the report body but the report has no data set.  Data regions are not allowed in reports without datasets.

    So, I'm very confused. If I am going to assign a DataTable dynamically (at runtime), do I still need to have the DataSetName property of the report table assigned to a datatable? I think I do, since how would the report know about the fields otherwise at design? But how can I get away from this situation in which I get errors if I assign a DataTable and I get errors if I don't. How DO I set things up so that I can assign a report to a ReportViewer and a DataTable to the LocalReport in code?

    Thank you very much for your help.

    Monday, May 12, 2008 2:32 PM
  • User1869667223 posted

    Hi DesertDude,  

    How DO I set things up so that I can assign a report to a ReportViewer and a DataTable to the LocalReport in code?

    Dim rdsReport As New ReportDataSource
         With rdsReport
              .Name = "SalesReport"
              .Value = dtReport 'Created earlier in the code
         End With
         .LocalReport.DataSources.Clear()
         .LocalReport.DataSources.Add(rdsReport)

    The dataTable name you are passing not matching with rdlc dataTable name,

    A data source instance has not been supplied for the data source 'SalesReport_SalesReportTable'
     

    Change the name in ReportDataSource as "SalesReport_SalesReportTable"

    Dim rdsReport As New ReportDataSource
         With rdsReport
              .Name = "SalesReport_SalesReportTable"
              .Value = dtReport 'Created earlier in the code
         End With
         .LocalReport.DataSources.Clear()
         .LocalReport.DataSources.Add(rdsReport)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2008 1:35 AM
  • User755602676 posted

    Thanks, Sasi. That puzzled me at first, but it makes sense. The controls in the report are expecting a datatable by a certain name, right? Anyway, the report finally appeared in the ReportViewer for the first time, so this is progress. I'm not completely out of the woods, yet, but hopefully I can now get there from here.

    Thanks so much for your help. You have saved me countless hours. I've marked the Answer as you requested.

    Tuesday, May 13, 2008 5:24 PM
  • User750074654 posted

    Thank you for this reply. It has helped me in my project.

    Monday, June 24, 2013 11:02 AM