This is a migrated thread and some comments may be shown as answers.

Help Passing Parameters from Win Form

10 Answers 477 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brad Williams
Top achievements
Rank 1
Brad Williams asked on 11 Mar 2010, 03:14 PM
Can someone help me with code to put inside my Win Form vb.net that will allow me to pass a Parameter to my Q1 2010 report that will affect the SQL WHERE Clause?  I am creating a Purchase Order Report and need to pass the report the PO Number that the report needs to generate. 

This has to be the simpliest of all report questions and I fill dumb for asking but I can't find any help on telerik website.  I have spent hours looking. 

Thanks!

10 Answers, 1 is accepted

Sort by
0
fruzicka
Top achievements
Rank 1
answered on 11 Mar 2010, 05:54 PM
In your form just do simple:

report.ReportParameters.Add("myID", ReportParameterType.Integer, 123);

or

report.ReportParameters["myID"].Value = 123; //if parameter was created by designer

and in your report:

int id = (int)r.ReportParameters["myID"].Value; 

Hope this helps.
0
Brad Williams
Top achievements
Rank 1
answered on 11 Mar 2010, 06:06 PM
fruzicka,

Thank you for replying.  Thats sounds like it might work.  Problem for me is that code doesn't make since so I don't know how to implement it.

When I paste your code into my win form, I get an error.  I'm sure its I'm not exposing something or initiating something correctly to make it work.  I feel so stupid.  Then the part that goes into the report, I have no idea where to insert that.
0
Accepted
fruzicka
Top achievements
Rank 1
answered on 11 Mar 2010, 06:23 PM
//in your form
InvoiceReport inv = new InvoiceReport() //classname of your report
inv.ReportParameters.Add("InvNr", ReportParameterType.Integer, 123);

//to display your report
repView1.Report = inv;
repView1.RefreshReport();

//in your reports contstructor set
Datasource = null;

//then in NeedDataSourceEvent (is fired if datasource is null)
//get your parameter
int invNr = (int)ReportParameters["InvNr"].Value;

//if you have specified some sqlDataSource
sqlDataSource1.SelectCommand = "SELECT * FROM Invoice WHERE InvNr='" + invNr + "'";
DataSource = sqlDataSource1;

//and bind some controls to this datasource, see documentation for this
0
Brad Williams
Top achievements
Rank 1
answered on 11 Mar 2010, 07:02 PM
Ok, I made a little progress from my form side.  I had to modify some of your code to make it not error out.  Here is my code that is in my form:
___________________________________________________________________________
        Dim myNum As String = "808080"
        Dim myreport As New TagLabel
        myreport.ReportParameters.Add("ParamTagNum", ReportParameterType.Integer, myNum)
        ReportViewer1.Report = myreport
        ReportViewer1.RefreshReport()
___________________________________________________________________________

Here is the code that is in my Report  NeedDataSourceEvent currently:
___________________________________________________________________________

        Dim sql As String = ""
        sql = "SELECT * FROM myTable " & _
                 "WHERE  TagNum = @TAGNUM"

        Dim connectionString As SqlConnection
        connectionString = New SqlConnection("xxxxxxx")

        adapter.SelectCommand = New SqlCommand(sql, connectionString)
        adapter.SelectCommand.Parameters.Add(New SqlParameter("@TAGNUM", "199"))
        Dim dataSet As New DataSet()
        adapter.Fill(dataSet)
        Me.DataSource = dataSet
___________________________________________________________________________

I have no idea how to take your code and put into the NeedDataSourceEvent.

Your code:
int invNr = (int)ReportParameters["InvNr"].Value

That errors out.  invNr says its not declared. Which I know but don't know what it is that I am declairing.  Is it a string? Some other type of object? (int) doesn't accept that number of arguments.

0
fruzicka
Top achievements
Rank 1
answered on 11 Mar 2010, 07:07 PM
The .Value is of type object, so you have to cast it in type which you need.
Sorry for bugs in my code ... I wrote it in notepad ...
0
Brad Williams
Top achievements
Rank 1
answered on 11 Mar 2010, 07:20 PM
I greatly appreciate you trying to help me.

I am still lost as to how to modify my existing code in the report to accept this incoming parameter from my form. 
0
Accepted
fruzicka
Top achievements
Rank 1
answered on 11 Mar 2010, 07:32 PM
I just tried it:
string key = ReportParameters["key"].Value.ToString();
in NeedDataSource event works fine.

But you have to have some field (textbox...) bound to the datasource. If not, the event is not fired.

0
Brad Williams
Top achievements
Rank 1
answered on 11 Mar 2010, 08:07 PM
FINALLY!!!!!!!!

It works.  Thank you so much for your continued effort to help me.  I knew in the end it would be so simple. 

To summarize for anyone who might read this in the future:

In Win Form add this code:
        Dim myreport As New {NameOfYourReport}
        myreport.ReportParameters.Add("{NameOfYourParam}", ReportParameterType.string, "12345")
        ReportViewer1.Report = myreport
        ReportViewer1.RefreshReport()

In Report Class add this code to NeedDataSourceEvent"
        Dim mykey As String
        mykey = ReportParameters("{NameOfYourParam}").Value.ToString

Then just use the variable mykey to help build your SQL Select statement.

Why I couldn't find this code anywhere on Telerik website is crazy.



0
Frank
Top achievements
Rank 1
answered on 22 Apr 2016, 03:38 PM

I still can't find the documentation to send the parameters proper way from the winforms reportviewer to the report. 

The way it is showed here is no longer valid. 

0
Stef
Telerik team
answered on 25 Apr 2016, 12:59 PM
Hi Frank,

These posts are from 2010, where in Q2 2012 there is a change in the way reports are passed to viewers and ReportProcessor instances - How to migrate your project to utilize the new ReportSource objects.

The blog post elaborates on the report sources settings and includes links to online help articles.


In Visual Studio you can configure viewers' ReportSource properties through the available wizards - Load Report Dialog.

Regards,
Stef
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Brad Williams
Top achievements
Rank 1
Answers by
fruzicka
Top achievements
Rank 1
Brad Williams
Top achievements
Rank 1
Frank
Top achievements
Rank 1
Stef
Telerik team
Share this question
or