Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

Thanks
kishore
Posted
Updated 24-Sep-12 4:17am
v3

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.

Try!

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[^]
 
Share this answer
 
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
C#
//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
C#
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.To.Add(emailAddr);
				}
			}
			else
			{
				mail.To.Add(sendTo);
			}
			mail.Subject = subject;
			mail.Body = body;
			mail.IsBodyHtml = isHTML;

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

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

		    SmtpServer.Send(mail);  
 
Share this answer
 
Comments
Member 12870741 3-Mar-17 8:45am    
Hi there. Where did you add the code in ssrs?
Sorry for the stupid question but I'm struggling :)
David_Wimbley 3-Mar-17 18:52pm    
To be clear, this isn't SSRS code this is code to generate an SSRS report from an SSRS server by the rdl name.

This code can be slapped into a console application with adding a web reference to your SSRS report server report execution url

Member 12870741 7-Mar-17 3:47am    
Hi there. Thank you for the reply. I get it now :)

I am new to C# and VB and would like to create a button to render the report in Excel and then insert that attachment in Outlook. Trying to use some kind of custom code but not working LOL. I take it that I would have to develop an application to do this? Any suggestions of where to start? I am busy installing VS 2015. If you maybe have some links. I am really not lazy and eager to learn. I just want to know what I should do or start off with. you response would be much appreciated thanks you :)
Member 7909353 20-Mar-19 7:46am    
I need to pass parameters to this report when I click on a button in ssrs report(button mean link in parent rport)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900