Mark
Mark

Reputation: 8431

How to access session variable in SSRS (SQL Server 2012)?

I want to access the session or viewstate variables in SSRS 2012 reports just like we access in c#.

Session["user"]

I want to access like this in SSRS report.

I want to include the user who is logged in when the report is generated.

Is there any way to achieve this? Can we access it in custom code in SSRS?

Upvotes: 5

Views: 3866

Answers (3)

Dan Sorensen
Dan Sorensen

Reputation: 11753

In the code behind, you could copy the desired data at a report parameter.

    // Copy the session username to a report param called "CurrentUser".
    ReportParameter userParam = new ReportParameter("CurrentUser", Session["user"]);

    // add the parameter to the report
    reportViewer.ServerReport.SetParameters(new ReportParameter[] { userParam });

And then add your parameter to the report as you would any other parameter. See example at: http://msdn.microsoft.com/en-us/library/aa337091.aspx

Upvotes: 1

Paul Coghill
Paul Coghill

Reputation: 677

If there's a lot of common data you could store the session data in a temporary table in the database. You update this session data table before you run the report. Then you can just access this data like any other table in the report, specifing the just the user ID as a parameter.

Upvotes: 0

Ian Preston
Ian Preston

Reputation: 39566

In the report itself you can reference the user running the report with User!UserID.

See Built-in Globals and Users References.

Edit after comment:

Since it seems that you have no internal reference to the user running the report, your best option might be to pass the user reference in with a Hidden Parameter.

From Books Online:

You can hide the parameter on the parameter input area of the published report, yet set values for it on a report URL or in a subscription definition.

So in this case it would mean that the parameter would never be visible to users, but you could set it programmatically.

You could pass a string based on Session["user"], or however you're managing user IDs outside the report, to the report then display/manipulate this parameter as required in the report.

Upvotes: 1

Related Questions