Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to send report using window service.
I have report server from where get a report on report viewer.
I have done in window form but I want to use this report in window service to send email as attachment.

My code is
C#
public class Service1 : System.ServiceProcess.ServiceBase
	{
protected override void OnStart(string[] args)
    {
ViewReport viewer=new ViewReport(); 
    
                ViewReport viewer=new ViewReport(); 
    
                   reportName = "DailyAttendanceReport";
                            viewer.GetConnectionOfReportServer();
                            viewer.getReportFrmServer(reportName, param);
                            viewer.Visible = true;
                            viewer.ProcessingMode = ProcessingMode.Local; 
                           
                            Thread th = new Thread(() => SendEmail("abc@gmail.com", "def@gmail.com", "password", "Automatic Mail sending", "Successfully working contact", "Attendence.xls"));
                            th.Start();
                   
 }
}


C#
public  class ViewReport:ReportViewer
   {
     private ReportViewer reportViewer1=new ReportViewer();
     bool flag = true;
     string[] ldata = new string[2];


public void GetConnectionOfReportServer()
     {
        try
          {
              NetworkCredential credential = new NetworkCredential("administrator", "password", "database");
              this.reportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials = credential;

              //select where the report should be generated with the report viewer control or on the report server using the SSRS service.
              this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote;
              this.reportViewer1.ServerReport.ReportServerUrl = new Uri(@"http://xxx.xxx.xxx.xxx/ReportServer");
          }

          catch (Exception ex)
          {
              
          }


     }
public void getReportFrmServer(string report, string[] param)
     {
          if (flag)
          {
              reportName = report;
              flag = false;
          }

          try
          {
              this.reportViewer1.ServerReport.ReportPath = "/Report From_Iris_New/" + report;
              if (report == "DailyAttendanceReport")
              {
                  List<ReportParameter> myParams = new List<ReportParameter>();
                  myParams.Add(new ReportParameter("dailyDate", param[0]));
                  ReportParameter p = new ReportParameter("contractorname");
                  if (param[1] == "null")
                  {
                      p.Values.Add(null);
                      myParams.Add(p);
                  }
                  else
                  {
                      p.Values.Add(param[1]);
                      myParams.Add(p);
                  }
                  ReportParameter q = new ReportParameter("sitename");
                  if (param[2] == "null")
                  {
                      q.Values.Add(null);
                      myParams.Add(q);
                  }
                  else
                  {
                      q.Values.Add(param[2]);
                      myParams.Add(q);
                  }
                  this.reportViewer1.ServerReport.SetParameters(myParams);  //here this proccess stop
              
              }


             this.reportViewer1.RefreshReport();
         }
         catch (Exception exception)
         {
            
         }

     }
}


reportViewer1generates report but I want to send this reoprt as attachment.
I do not want to view report.
Posted
Updated 7-Jul-13 20:16pm
v4

1 solution

C#
byte[] file= viewer.getReportFrmServer(reportName, param);

                          MemoryStream s = new MemoryStream(file);
                          s.Seek(0, SeekOrigin.Begin);
                          Attachment a = new Attachment(s, "myReport.pdf");
 Thread th = new Thread(() => SendEmail("abc@gmail.com", "def@gmail.com", "password", "Automatic Mail sending", "Successfully working contact", "Attendence.xls"));
                            th.Start();






C#
public void getReportFrmServer(string report, string[] param)
     {
          if (flag)
          {
              reportName = report;
              flag = false;
          }

          try
          {
              this.reportViewer1.ServerReport.ReportPath = "/Report From_Iris_New/" + report;
              if (report == "DailyAttendanceReport")
              {
                  List<ReportParameter> myParams = new List<ReportParameter>();
                  myParams.Add(new ReportParameter("dailyDate", param[0]));
                  ReportParameter p = new ReportParameter("contractorname");
                  if (param[1] == "null")
                  {
                      p.Values.Add(null);
                      myParams.Add(p);
                  }
                  else
                  {
                      p.Values.Add(param[1]);
                      myParams.Add(p);
                  }
                  ReportParameter q = new ReportParameter("sitename");
                  if (param[2] == "null")
                  {
                      q.Values.Add(null);
                      myParams.Add(q);
                  }
                  else
                  {
                      q.Values.Add(param[2]);
                      myParams.Add(q);
                  }
                  this.reportViewer1.ServerReport.SetParameters(myParams); 

              }


           byte[] returnValue = null;
             string format = "PDF";
             string deviceinfo = "";
             string mimeType = "";
             string encoding = "";
             string extension = "pdf";
             string[] streams = null;
             Warning[] warnings = null;

             returnValue = reportViewer1.ServerReport.Render(format, deviceinfo, out mimeType, out encoding, out extension, out streams, out warnings);
             this.reportViewer1.RefreshReport();
             return returnValue;
         }
         catch (Exception exception)
         {

         }

     }
 
Share this answer
 
Comments
Sushil Mate 8-Jul-13 4:35am    
so you posted question & you answer that & then you accepted this solution. can you please tell us what exactly the problem was. might be help other OP.

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