Help Passing Parameters from Win Form

11 posts, 2 answers
  1. Brad Williams
    Brad Williams avatar
    8 posts
    Member since:
    Feb 2010

    Posted 11 Mar 2010 Link to this post

    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!
  2. fruzicka
    fruzicka avatar
    27 posts
    Member since:
    Oct 2012

    Posted 11 Mar 2010 Link to this post

    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.
  3. DevCraft banner
  4. Brad Williams
    Brad Williams avatar
    8 posts
    Member since:
    Feb 2010

    Posted 11 Mar 2010 Link to this post

    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.
  5. Answer
    fruzicka
    fruzicka avatar
    27 posts
    Member since:
    Oct 2012

    Posted 11 Mar 2010 Link to this post

    //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
  6. Brad Williams
    Brad Williams avatar
    8 posts
    Member since:
    Feb 2010

    Posted 11 Mar 2010 Link to this post

    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.

  7. fruzicka
    fruzicka avatar
    27 posts
    Member since:
    Oct 2012

    Posted 11 Mar 2010 Link to this post

    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 ...
  8. Brad Williams
    Brad Williams avatar
    8 posts
    Member since:
    Feb 2010

    Posted 11 Mar 2010 Link to this post

    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. 
  9. Answer
    fruzicka
    fruzicka avatar
    27 posts
    Member since:
    Oct 2012

    Posted 11 Mar 2010 Link to this post

    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.

  10. Brad Williams
    Brad Williams avatar
    8 posts
    Member since:
    Feb 2010

    Posted 11 Mar 2010 Link to this post

    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.



  11. Frank
    Frank avatar
    86 posts
    Member since:
    Mar 2012

    Posted 22 Apr in reply to Brad Williams Link to this post

    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. 

  12. Stef
    Admin
    Stef avatar
    3036 posts

    Posted 25 Apr Link to this post

    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
Back to Top
DevCraft banner