Click here to Skip to main content
12,764,666 members (38,749 online)
Rate this:
Please Sign up or sign in to vote.
Hi Friends,

I need an urgent help from you.

I designed a Report using SQL Server 2008 Business Intelligence Development Studio (SSRS). I need to pass parameters to this report when I click on a button in the ASP.NET webpage and I also need to send this report in email to my Admin department.

Please help me. ASAP.

Posted 24-Sep-12 0:27am
Updated 24-Sep-12 5:17am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I need to pass parameters to this report when I click on a button in the ASP.NET webpage and I also need to send this report in email
Go step wise.
1. Pass parameters to SSRS on a button click
2. Send an email on button click
3. Create a PDF export of SSRS on button click
4. Merge step 1,2 & 3 into one.


Few tips that can help:
Sending an Email in C# with or without attachments: generic routine.[^]
MSDN: Add a Filter (Report Builder and SSRS)[^]
Adding Filter Parameters to SQL Server 2008 Reporting Services Reports[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The below code has been plucked from various files which is what i use to generate SSRS reports.

The things im not spelling out here are how to add web references to your project in VS, that is adding reference to your SSRS web service URLS.

Ive also attached code to send an email with an attachment, i would suggest altering the code here but creating classes that will be more conducive to what it is you are trying to do.

The code i've provided "should" run, but keep in mind i may have forgotten something, but this should get you started and on the right track.

Code to generate SSRS report
//Init Vars
private static string fileName = null;
private static string historyID = null;
private static string deviceInfo = null;
private static string format = "PDF";
private static string encoding = null;
private static string mimeType = null;
private static string extension = null;
private static string[] streamIDs = null;
private static string _reportName = null;
private static string _historyID = null;
private static bool _forRendering = false;
private static Byte[] results;
private static report.Service.Reference reportService;
private static report.Execution.Reference reportExecReference;
//Inside some method, place this code
fileName      = destinationFilePath; 
historyID     = null;
deviceInfo    = null;
format        = (Path.GetExtension(destinationFilePath).ToUpper()).Replace(".", "");
encoding      = null;
mimeType      = null;
extension     = null;
streamIDs     = null;
_reportName   = reportName; 
_historyID    = null;
_forRendering = false;

reportService = new report.Service.Reference.ReportingService2005();
reportExecReference = new report.Execution.Reference.ReportExecutionService();

reportService.Credentials = new NetworkCredential("username", "password", "DOMAIN");
reportServiceExec.Credentials = new NetworkCredential("username", "password", "DOMAIN");

reportService.Url = String.Format(@"http://{0}/reportserver/reportservice2005.asmx", iniDatabaseServer);
reportServiceExec.Url = String.Format(@"http://{0}/reportserver/reportexecution2005.asmx", iniDatabaseServer);

historyID = null;
deviceInfo = null;
format = Path.GetExtension(destinationFilePath) == ".xls" ? "Excel" : "PDF";
encoding = String.Empty;
mimeType = String.Empty;
extension = String.Empty;
report.Execution.Reference.Warning[] warnings = null;
streamIDs = null;
fileName = destinationFilePath;
_reportName = reportName;
_historyID = null;
_forRendering = false;
report.Service.Reference.ParameterValue[] _values = null;
report.Service.Reference.DataSourceCredentials[] _credentials = null;
report.Service.Reference.ReportParameter[] _parameters = null;
_parameters = reportService.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials);
report.Execution.Reference.ExecutionInfo ei = reportServiceExec.LoadReport(_reportName, historyID);
report.Execution.Reference.ParameterValue[] parameters = new report.Execution.Reference.ParameterValue[parameterName.Count];

if (_parameters.Length > 0)
	for (int i = 0; i < parameterName.Count; i++)
		parameters[i] = new report.Execution.Reference.ParameterValue();
		parameters[i].Label = null;
		parameters[i].Name = parameterName[i];
		parameters[i].Value = parameterValue[i];
reportServiceExec.SetExecutionParameters(parameters, "en-us");

results = reportServiceExec.Render(format, deviceInfo,
		  out extension, out encoding,
		  out mimeType, out warnings, out streamIDs);

using (FileStream stream = File.OpenWrite(fileName))
	stream.Write(results, 0, results.Length);

Code to send an email with an attachment
MailMessage mail = new MailMessage();
			SmtpClient SmtpServer = new SmtpClient(smptServer, portNumber);
			mail.From = new System.Net.Mail.MailAddress(sendFrom);

			if (sendTo.Contains(";"))
				string[] emailAddress = sendTo.Split(';');

				foreach (string emailAddr in emailAddress)
			mail.Subject = subject;
			mail.Body = body;
			mail.IsBodyHtml = isHTML;

			System.Net.Mail.Attachment attachment;
			attachment = new System.Net.Mail.Attachment(attachmentPath);

			SmtpServer.Port = portNumber;
			SmtpServer.Credentials = new System.Net.NetworkCredential(authUser, authPass);
			SmtpServer.EnableSsl = true;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100