Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Generating Charts from Data in MMSQL and sending them as attachment (PDF) in email

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 679
    Answer it

    I am currently in a state of doubt whether I am doing the things in the right way. Basically, I have a task where data which is stored in a normalised db in MSSQL needs to be queried for so as to display them in different charts. These charts then needs to be converted in a custom PDF and are to be sent as an email attachment automatically.

     

    What I am doing: I am currently querying the data using LINQ, modifying the data to make them in JSON structure in the server side. Then in the client side, I am using AJAX to consume the JSON data and displaying it using google charts. Then, I am using jsPDF to generate the PDF by clicking a button.

     

    The solution I currently have works fine, however it is slightly manual, in the sense that I need to click a button to generate the PDF and send email. Do you recommend a better workflow to get the data, produce them as charts (even if these are done in server side) and send them as an email in one automatic process? I never used SSIS, SSRS and SSAS, so these might be good to look into. 

    Any feedback on this please?

 1 Answer(s)

  • Hi Andrew,
    You are using asp.net so you can use RDLC for your reporting. Now if you want to know about RDLC or how to work with RDLC in Asp.net take a look at following link:
    Creating RDLC Report in Asp.Net

    You can save those reports as PDF manually(as there is an option in the report viewer) or by code. I am sharing some code snippets which you can use for your solution.
    For converting your reports to a PDF:

      private string ExportReportToPDF(string reportNamewithFullPath)
        {
           Warning[] warnings;
           string[] streamids;
           string mimeType;
           string encoding;
           string filenameExtension;
           byte[] bytes = ReportViewer1.LocalReport.Render(
              "PDF", null, out mimeType, out encoding, out filenameExtension,
               out streamids, out warnings);
    
           string filename = Path.Combine(Path.GetTempPath(), reportNamewithFullPath);
           using (var fs = new FileStream(filename, FileMode.Create))
           {
              fs.Write(bytes, 0, bytes.Length);
              fs.Close();
           }
    
           return filename;
        }
    

    For sending that pdf via mail:

    private void BtnSendByMail_Click(object sender, EventArgs e)
    {
          Mail mail = sendMailView.CurrentItem;//some mail helper class. you can use your own
          mail.AttachmentFiles.Add(ExportReportToPDF(yourRdlcReportPath));
          mail.DeleteFilesAfterSend = true;
          mail.RequireAutentication = true;
          mail.Send();
    }
    

    So these are the two methods which you can combine to create complete code as per your requirement.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: