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

Programatic Parameter Assignment Help Issue

2 Answers 56 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Daniel Dillard
Top achievements
Rank 1
Daniel Dillard asked on 01 Mar 2010, 03:38 PM
I know this issue has been addressed a few times, but I'm having issues still with my attempt. I'm trying to avoid using the wizard for my dataset and do everything programatically.

Here's what I have in the report code-behind:
Public Sub New()  
        InitializeComponent()  
         Try  
 
            Dim connMAS As SqlConnection = New SqlConnection()  
 
            Dim cmd As SqlCommand  
            Dim commLoadPortalUserInfo As SqlCommand = connMAS.CreateCommand()  
            connMAS.ConnectionString = Convert.ToString("some connection string")  
 
            connMAS.Open()  
            cmd = connMAS.CreateCommand()  
 
            cmd.CommandText = "storedprocedurename" 
            cmd.CommandType = CommandType.StoredProcedure  
 
            cmd.Parameters.Add("@CompanyID", SqlDbType.VarChar)  
            cmd.Parameters("@CompanyID").Value = Me.ReportParameters("CompanyID").Value  
 
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)  
            Dim ds As DataSet = New DataSet  
 
            da.Fill(ds)  
            Me.DataSource = ds 
 
            connMAS.Close()  
              
        Catch ex As System.Exception              
            System.Diagnostics.Debug.WriteLine(ex.Message)  
        End Try  
    End Sub 
And here's what I have on my ASPX page calling the report:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        If (Not Page.IsPostBack) Then  
            ReportViewer1.Report = New RosterReport()  
            ReportViewer1.Report.ReportParameters(0).Value = "Value" 
              
            ReportViewer1.RefreshReport()  
        End If  
 
    End Sub  
End Class 
And the parameter value never makes it to the report, so I get a blank one. If I execute the report and statically replace the Me.Parameter.Value with a static string value, everything works perfectly.

I've also tried many other variations of the code above and even tried creating a new instantce of my RosterReport and then binding it to the reportviewer to no avail.

Edit: It seems like the initial report is getting instantiated, but a version of the report with the parameter values assigned is never created. I thought that was the purpose of "refresh report"

Any help would be greatly appreciated.

2 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 02 Mar 2010, 05:19 PM
Hi Daniel,

You can't access parameters in the report constructor because they are not set until after the instance is created. This is not something specific to Telerik Reporting, but applicable for any .NET class - it is like creating a property in your class and trying to use its value in the class constructor.

Kind regards,
Steve
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
0
John Cobb
Top achievements
Rank 1
answered on 02 Mar 2010, 10:52 PM
Use the NeedDataSource Event for the report.  The report parameters are available there.  Be sure to set the datasource = null in the constructor.  Move all your sql code into the new eventhandler and you should be good to go.
Tags
General Discussions
Asked by
Daniel Dillard
Top achievements
Rank 1
Answers by
Steve
Telerik team
John Cobb
Top achievements
Rank 1
Share this question
or