views:

19

answers:

3

I have a report that shows all errors (if there are any) from a process that runs every day.
At the end of the process, I want to write some code to execute the report and email it. I am seeing how to email a report from code, but I can't seem to find anywhere that shows how to run the report from code.
I am using C# in vs 08, and the report is from ssrs 08. Any assistance would be greatly appreciated!

A: 

Here is some vb code to render the report into bytes:

Private Sub ExportReport(ByVal ReportViewer)
    reportType = "PDF"
    deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat><PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>0.5in</MarginLeft><MarginRight>0.5in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
    renderedBytes = ReportViewer.LocalReport.Render(reportType, deviceInfo, mimeType, Encoding, fileNameExtension, streams, warnings)
End Sub


Public Function CreateAttachment() As Attachment
    Dim MemStr As New MemoryStream(renderedBytes)
    Dim ContentType As New ContentType(mimeType)
    Dim pdf As New Attachment(MemStr, ContentType)
    pdf.Name = ReportName & "." & fileNameExtension
    Return pdf
End Function

this shows the basic stuff for rendering a report in code. VS 2005 code.

Decker97
I see that you are using a reportviewer. I don't have a user interface - i need to know how to render/call a report without a reportViewer.
sher1000
I'm using the report viewer non-visually, just to render the output. You can declare one and create without a form. Is the ssrs report a local mode or server mode report?
Decker97
A: 

if you aren't using ReportViewer, then call the Reporting Service web server (not report manager), and then email it.

gbn
A: 

I ended up creating a new subscription to the report which creates a job. I ran the job from code that executes the job(Taken from Microsoft)

Like this:

SqlConnection jobConnection; SqlCommand jobCommand; SqlParameter jobReturnValue; SqlParameter jobParameter; int jobResult;

        jobConnection = new SqlConnection("myconnectionstring");
        jobCommand = new SqlCommand("sp_start_job", jobConnection);
        jobCommand.CommandType = System.Data.CommandType.StoredProcedure;

        jobReturnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
        jobReturnValue.Direction = ParameterDirection.ReturnValue;
        jobCommand.Parameters.Add(jobReturnValue);

        jobParameter = new SqlParameter("@job_name", SqlDbType.VarChar);
        jobParameter.Direction = ParameterDirection.Input;
        jobCommand.Parameters.Add(jobParameter);
        jobParameter.Value = "name of the subscription job";
        jobConnection.Open();
        jobCommand.ExecuteNonQuery();
        jobResult = (Int32)jobCommand.Parameters["@RETURN_VALUE"].Value;
        jobConnection.Close();

        switch (jobResult)
        {
            case 0:
                Console.WriteLine("SQL Server Agent job, RunSISSPackage, started successfully.");
                break;
            default:
                Console.WriteLine("SQL Server Agent job, RunSISSPackage, failed to start.");
                break;
        }
        Console.Read();

By calling the job- which is a subscription - the report was rendered and emailed to the subscription recipients.

sher1000