locked
Passing Parameters from ASP.NET page RRS feed

  • Question

  • Hi there,

    I have asp.net page with Multi select listbox (Northwind customers table)
    I created report on the server called Customers (patamerer = In(@Company))

    When user pick few customers name and click submit the page store the values on label
    And then pass into the report with the following code

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    'Get Customers from Listbox multi select      
     Dim i As Integer
            Dim s As String = Trim(Label1.Text)
            Label1.Text = " "
            For i = 0 To ListBox1.Items.Count - 1
                If ListBox1.Items(i).Selected Then
                    Label1.Text = Label1.Text & ListBox1.Items(i).Text & vbCrLf
                End If
            Next
           'Pass multi value into the report
            Dim colRP As New System.Collections.Generic.List(Of Microsoft.Reporting.WebForms.ReportParameter)
            colRP.Add(New Microsoft.Reporting.WebForms.ReportParameter("Company", UCase(Label1.Text.TrimStart)))
            ReportViewer1.ServerReport.ReportServerUrl = New Uri("http://localhost/ReportServer")
            ReportViewer1.ServerReport.ReportPath = "/Northwind/Customers"
            ReportViewer1.ServerReport.SetParameters(colRP)
            ReportViewer1.ServerReport.Refresh()
        End Sub

    Now everything works fine when the ShowParameterPrompts is = True
    Then you need to click View Report button

    But when the ShowParameterPromts is = False
    The report is not render (the parameters value are there)

    My goal is to not show the Parameter on the report instead
    Pass the parameter directly from asp.net

    How do I make the report render? after the parameters passed

    Thanks
    Oded Dror

     

    Sunday, December 26, 2010 12:03 PM

Answers

  • Hi Oded Dror,

    As rparge posted, we can use the ReportParameter Constructor (String, String[]) to initiate a report parameter with multiple values by a string array. We can also generate the array dynamically based the selected items in a Listbox.

    For example, please refer to the code below:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
    	ReportViewer1.ServerReport.ReportServerUrl = New Uri("http://localhost/ReportServer")
    	ReportViewer1.ServerReport.ReportPath = "/FolderName/ReportName"
    	Dim companies As New List(Of String)()
    
    	For Each item As ListItem In ListBox1.Items
    		If item.Selected Then
    			companies.Add(item.Value)
    		End If
    	Next
    
    	Dim p As New ReportParameter("Company", companies.ToArray())
    
    	ReportViewer1.ServerReport.SetParameters(p)
    	ReportViewer1.ServerReport.Refresh()
    
    End Sub 
    
    

     

    Please correct the ReportServerUrl, ReportPath and control names in the code.

    Thanks,
    Tony Chain

     


    Tony Chain [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Tony Chain Wednesday, December 29, 2010 12:24 PM
    • Marked as answer by odeddror Wednesday, December 29, 2010 12:43 PM
    Wednesday, December 29, 2010 8:42 AM

All replies

  • Oded shalom

    We call reports in  that way, see if that helps you

        Dim RetVal As Long

        RetVal = Shell("c:\progra~1\Intern~1\iexplore.exe http://localhost/ReportServer/Pages/ReportViewer.aspx?%2fFolderReports%2frptReportName&rs%3aCommand=Render&ParamName=" & param_variable, vbMaximizedFocus)


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Sunday, December 26, 2010 12:25 PM
  •   if (!IsPostBack)
      {
       List<ReportParameter> paramList = new List<ReportParameter>();
       Uri u = new Uri(ConfigurationManager.AppSettings["ReportServerURL"]);
    
       ReportViewer1.ServerReport.ReportServerUrl = u;
       ReportViewer1.ServerReport.ReportPath = Context.Items["ReportNameAndPath"].ToString();
    
       ReportParameterInfoCollection parameters;
    
       parameters = ReportViewer1.ServerReport.GetParameters();
    
       for (int i = 0; i < parameters.Count; i++)
       {
        if (parameters[i].Name.ToLower() == "salesorderid")
        {
         paramList.Add(new ReportParameter("SalesOrderID", "44132", false));
        }
       }
    
       ReportViewer1.ServerReport.SetParameters(paramList);
    
      }
    
    

    When this code runs, the viewer does not prompt for a parameter. I have set "ShowParameterPrompts" to false also.

    Monday, December 27, 2010 12:06 AM
  • Uri,

    This works for 1 param, when you select more than one it came blank

    even if I did

     UCase(Label1.Text.TrimStart) & vbCrLf
    It now show Also it open a new IE browser and I want to be in the same page.
    Thanks,
    Oded
    Monday, December 27, 2010 1:50 PM
  • Rparge,

    This is what I did still not show


            If Not IsPostBack Then
                Dim paramList As New List(Of ReportParameter)()
                Dim u As New Uri(ConfigurationManager.AppSettings("http://localhost/ReportServer"))

                ReportViewer1.ServerReport.ReportServerUrl = u
                ReportViewer1.ServerReport.ReportPath = Context.Items("/Northwind/Customers").ToString()

                Dim parameters As ReportParameterInfoCollection

                parameters = ReportViewer1.ServerReport.GetParameters()

                For i As Integer = 0 To parameters.Count - 1
                    If parameters(i).Name.ToLower() = "Company" Then
                        paramList.Add(New ReportParameter("Company", UCase(Label1.Text.TrimStart), False))
                    End If
                Next
                ReportViewer1.ServerReport.SetParameters(paramList)
            End If

    As you can see is very simillar of what I wrote

    Thanks,

    Oded Dror

     

    Monday, December 27, 2010 1:53 PM
  • Imports Microsoft.Reporting.WebForms
    
    Partial Class _Default
      Inherits System.Web.UI.Page
    
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
    
          Dim i As Integer = 0
          Dim paramList As New List(Of ReportParameter)()
    
          Dim u As Uri = New Uri("http://<your server>/Reportserver")
    
          ReportViewer1.ServerReport.ReportServerUrl = u
          ReportViewer1.ServerReport.ReportPath = "/AdventureWorks 2008 Sample Reports/Sales Order Detail 2008"
    
          Dim parameters As ReportParameterInfoCollection
    
          parameters = ReportViewer1.ServerReport.GetParameters()
    
          For i = 0 To parameters.Count - 1
    
            If parameters(i).Name = "SalesOrderID" Then
              paramList.Add(New ReportParameter("SalesOrderID", "44132", False))
            End If
    
          Next i
    
          ReportViewer1.ServerReport.SetParameters(paramList)
    
        End If
    
      End Sub
    End Class
    

    Try this, I changed it to VB and to use AdventoreWorks, I do not get prompted for params using the above.

    Cheers!

    Rick

    Monday, December 27, 2010 7:13 PM
  • Rick,

    Again it works with one value

     If parameters(i).Name = "Company" Then
                        paramList.Add(New ReportParameter("Company", Label2.Text.Trim, False))
                    End If

    if you select multiple Companies from the listbix it won't show (it show only if you picked one)

    Thanks,

    Oded Dror

     

    Tuesday, December 28, 2010 1:40 PM
  • Oded

    Do  you mean to select  multiple values for that parameter or more than one parameter?


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Tuesday, December 28, 2010 1:55 PM
  • Oded

    Do  you mean to select  multiple values for that parameter or more than one parameter?


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Tuesday, December 28, 2010 1:55 PM
  • Uri,

    Sorry I ment values (multiple companies)

    Thanks,

    Tuesday, December 28, 2010 2:58 PM
  • Oh, OK, you need to pass an array to the parameter.

    This works for me.

    Dim paramValues(2) As String
    paramValues(0) = "44132"
    paramValues(1) = "45579"
    
    parameters = ReportViewer1.ServerReport.GetParameters()
    
    For i = 0 To parameters.Count - 1
     If parameters(i).Name = "SalesOrderID" Then
       paramList.Add(New ReportParameter("SalesOrderID", paramValues, False))
    End If

    http://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.reportparameter.reportparameter(v=VS.90).aspx

    This shows the overloads for the parameter.

     

    • Proposed as answer by Tony Chain Wednesday, December 29, 2010 5:16 AM
    Tuesday, December 28, 2010 4:44 PM
  • Rick,

    You show me fixed Value and I need dynamic param because the user picked different companies name all the time

    I don't know which companies the user select?

    And don't forget it store on a label and then pass it to the report

    Thanks,

    Oded Dror

     

    Tuesday, December 28, 2010 6:02 PM
  • Getting the values that have been entered is up to you, but what I'm showing here is that you need to get the customer inputs, and populate an array with these values, and then pass that array to the parameter.

    Does that help?

     

    Tuesday, December 28, 2010 8:56 PM
  • Rick,

    I did

    Public

     

    Function listCount() As Integer

     

    Dim li As ListItem

     

    Dim x As Integer = 0

     

    For Each li In ListBox1.Items

     

    If li.Selected = True Then

    x = x + 1

     

    End If

     

    Next

    Label3.Text = x

     

    End Function

    to get the number of array I need

    Protected

     

    Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

     

    Dim i As Integer = 0

     

    Dim paramList As New List(Of ReportParameter)()

     

    Call listCount()

     

    'This is Array but fixed value not dynamic

     

    Dim paramValues(Label3.Text) As String

    paramValues(0) =

    "Alfreds Futterkiste"

    paramValues(1) =

    "Around the Horn"

    paramValues(2) =

    "My Company"

    How do I convert the Label2 "Alfreds Futterkiste Around the Horn My Company" to array?

    This is label2

     

    'Get Customers from Listbox multi select

     

    Dim j As Integer

     

    Dim s As String = Trim(Label1.Text)

    Label1.Text =

    " "

     

    For j = 0 To ListBox1.Items.Count - 1

     

    If ListBox1.Items(j).Selected Then

    Label1.Text = Label1.Text & ListBox1.Items(j).Text & vbCrLf

     

    End If

     

    Next

    Label2.Text = Left(Label1.Text, (Len(Label1.Text) - 0))

    Thanks,

    Oded Dror


    Thanks, Oded Dror
    Tuesday, December 28, 2010 9:38 PM
  • Hi Oded Dror,

    As rparge posted, we can use the ReportParameter Constructor (String, String[]) to initiate a report parameter with multiple values by a string array. We can also generate the array dynamically based the selected items in a Listbox.

    For example, please refer to the code below:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
    	ReportViewer1.ServerReport.ReportServerUrl = New Uri("http://localhost/ReportServer")
    	ReportViewer1.ServerReport.ReportPath = "/FolderName/ReportName"
    	Dim companies As New List(Of String)()
    
    	For Each item As ListItem In ListBox1.Items
    		If item.Selected Then
    			companies.Add(item.Value)
    		End If
    	Next
    
    	Dim p As New ReportParameter("Company", companies.ToArray())
    
    	ReportViewer1.ServerReport.SetParameters(p)
    	ReportViewer1.ServerReport.Refresh()
    
    End Sub 
    
    

     

    Please correct the ReportServerUrl, ReportPath and control names in the code.

    Thanks,
    Tony Chain

     


    Tony Chain [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Tony Chain Wednesday, December 29, 2010 12:24 PM
    • Marked as answer by odeddror Wednesday, December 29, 2010 12:43 PM
    Wednesday, December 29, 2010 8:42 AM
  • Tony,

    It works, Thank you very much

    I can fly with reporting services now

    Thnaks again

    Say thanks to Rparge also

    Oded Dror

     


    Thanks, Oded Dror
    Wednesday, December 29, 2010 12:50 PM