Emad-ud-deen
Emad-ud-deen

Reputation: 4864

Looking to call a SSRS report with a parameter from VB.Net code

I have a SSRS report that contains a parameter called @playerID

Can you show me what coding I need to add so the report can be run using the parameter?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim reportParameters() As Int16 = {10}

    Me.ReportViewer1.ServerReport.SetParameters(reportParameters)
End Sub

When I run the report, no data is shown unless I comment out the 2nd line of code and use RefreshReport which in that case it will prompt me to input the parameter with a 10 and that works.

Upvotes: 1

Views: 6324

Answers (1)

This code will loop through an array of parameters and add them to a reportviewer. You should make a getParameterName(int) method to get the names for the rv.

int[] parameters = new int[1];
parameters[0] = 10;

//Create Parameter Collection Array
Microsoft.Reporting.WebForms.ReportParameter[] reportParameterCollection = new       Microsoft.Reporting.WebForms.ReportParameter[parameters.Length];
//Loop through each parameter and load the name and value into the parameter collection
int i = 0;
foreach (int parameter in parameters)
{
   reportParameterCollection[i] = new Microsoft.Reporting.WebForms.ReportParameter();
   reportParameterCollection[i].Name = getParameterName(i);
   reportParameterCollection[i].Values.Add((parameters[i]);
   i++;
}

//Load Parameters into the ReportViewer
ReportViewer1.ServerReport.SetParameters(reportParameterCollection);
ReportViewer1.ServerReport.Refresh();

More of a C# guy myself, but here's my hand at vb.net in 5 seconds:

Dim paramList As New Generic.List(Of ReportParameter)

paramList.Add(New ReportParameter("Report_Parameter1","")

Me.ReportViewer1.LocalReport.SetParameters(paramList)
ReportViewer1.RefreshReport()

Upvotes: 1

Related Questions