A Simple Report with Visual Studio and ReportViewer

General topics that don't fit into other forum categories

A Simple Report with Visual Studio and ReportViewer

Postby finaquant » 30 Dec 2013, 15:19

You can download the Visual Studio (2012) file for running the report explained here at:
Downloads for C#/.NET Developers

This simple report demonstrates:

  • How to create a simple report with ReportViewer
  • How to add parameters to a report (@Dealer in SQL filter of DataSet)
  • How to bind a DataSet to a report as data source programmatically (dynamic binding)
  • How to export a report to a PDF/WORD file programmatically (without preview)
  • How to send an Email with (PDF/WORD) attachment from an SMTP email account
Image

Useful links for common tasks:
  1. Walkthrough: Creating a ReportViewer Report (steps for creating a report in Visual Studio)
  2. Walkthrough: Assign Dataset Dynamically Created in Code to your local report with ReportViewer
  3. Export ReportViewer to a pdf or word file
  4. How to change DataTables in rdlc reports programmatically

How to run the report?
First, download and install finaquant® calcs (.NET Calculation Engine based on Table Functions) which comes with 30-day free trial period. You will need this library for creating and storing test data.

Open the Visual Studio project named BasicReport and press F5.

You have two options for viewing the simple report: Dynamic and Static data source binding. By commenting one, and commenting out the other you can try both in sequence.
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Report with dynamic DataSet binding
  2. // Application.Run(new Form1(ds, "Sales Report, dynamic DataSet binding"));
  3.  
  4. // Report with table data from DataSet1
  5. string Dealer = "Merlin";
  6. Application.Run(new Form1(Dealer, "Sales Report, data from static DataSet1"));
GeSHi ©


Following method creates the test tables (sales, costs, margin1 and margin2) and stores them in the database named ReportDB.mdf (server based database). You can comment this statement out to save processor time once you have generated and stored the test tables as basis for reporting.
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // create & store data tables
  2. Test.Create_And_Store_Tables();
GeSHi ©


For sending an email with the report as PDF/WORD attachment you need to enter your SMTP email account information in the code file Program.cs:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // to addresses (update them)
  2. String ToEmail = "contact@DomainName.com";
  3. String ToName = "Finaquant Analytics";
  4. String CCEmail = "";
  5.  
  6. // set SMTP email account parameters (update them)
  7. string MailFromEmail = "support@YourDomain.com";
  8. string MailFromName = "Your Name";
  9. string MailPwd = "";
  10. string MailServer = "mail.YourDomain.com";
GeSHi ©


How to add parameters to a report
1) Double click on DataSet1.xsd in Solution Explorer to get the Design View
2) Select from right-mouse menu: Configure
Image

3) Add a parameter (@agent) as filtering criterion to SQL statement of TableAdapter
Image

4) Add a text box control (textBox1) to Form1 for the parameter dealer (@agent in SQL)
Image

5) Extend the methods in Form1.cs ..
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. public Form1(string Dealer, string Header = "Static Sales Report")
  2. {
  3.     InitializeComponent();
  4.  
  5.     this.Text = Header + " - finaquant.com";
  6.     this.textBox1.Text = Dealer;
  7. }
  8. private void Form1_Load(object sender, EventArgs e)
  9. {
  10. ...
  11.     this.DataTable1TableAdapter.Fill(this.DataSet1.DataTable1, this.textBox1.Text);
  12. ...
  13. }
GeSHi ©

.. so that the report can be called with the parameter dealer as follows:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Report with table data from DataSet1
  2. string Dealer = "Merlin";
  3. Application.Run(new Form1(Dealer, "Sales Report, data from static DataSet1"));
GeSHi ©


How to assign a DataSet to report programmatically? (dynamic data source binding)
Extend the methods in Form1.cs ..
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. private DataSet dset = null;
  2.  
  3. public Form1(DataSet ds, string Header = "Dynamic Sales Report")
  4. {
  5.     dset = ds;
  6.     InitializeComponent();
  7.     this.Text = Header + " - finaquant.com";
  8. }
  9. private void Form1_Load(object sender, EventArgs e)
  10. {
  11. ...
  12.     this.DataTable1BindingSource.DataSource = dset.Tables[0];
  13. ...
  14. }
GeSHi ©

.. so that the report can be called as follows:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Report with dynamic DataSet binding
  2. // Application.Run(new Form1(ds, "Sales Report, dynamic DataSet binding"));
GeSHi ©


How to export report to a pdf or word file?
Here is the C#/.NET code:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Export Report to a PDF/WORD file programmatically (without preview)
  2. LocalReport rep = new LocalReport();
  3. string ProjectPath = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
  4. string ReportFile = "Report1.rdlc";
  5. string ReportPath = ProjectPath + @"\" + ReportFile;
  6. rep.ReportPath = ReportPath;
  7.  
  8. ReportDataSource rds = new ReportDataSource();
  9. rds.Name = "DataSet1";
  10. rds.Value = ds.Tables[0];   // sales table
  11. rep.DataSources.Add(rds);
  12.  
  13. Byte[] mybytes = rep.Render("PDF");         // export to a PDF file
  14. // Byte[] mybytes = rep.Render("WORD");     // export to a WORD file
  15.  
  16. // replace file paths with your own on your computer
  17. String ExportFile = @"C:\Users\Tuncali\Documents\MyWORKfiles\SalesRep.pdf";     // PDF
  18. // String ExportFile = @"C:\Users\Tuncali\Documents\MyWORKfiles\SalesRep.doc";  // WORD
  19.  
  20. using (FileStream fs = File.Create(ExportFile))
  21. {
  22.     fs.Write(mybytes, 0, mybytes.Length);
  23. }
GeSHi ©


How to send a report file (pdf or word) as attachment to email?
Once you have created a report file as shown above, you can send it by email as attachment.
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Send Report with Email as attachment
  2.  
  3. // to addresses (update them)
  4. String ToEmail = "contact@DomainName.com";
  5. String ToName = "Finaquant Analytics";
  6. String CCEmail = "";
  7.  
  8. // set SMTP email account parameters (update them)
  9. string MailFromEmail = "support@YourDomain.com";
  10. string MailFromName = "Your Name";
  11. string MailPwd = "";
  12. string MailServer = "mail.YourDomain.com";
  13.  
  14. // subject & message
  15. string SubjectStr = "Sales Report";
  16. string MessageStr = "Sales Report is attached";
  17.  
  18. MailMessage mail;
  19. SmtpClient smtp;
  20.  
  21. try
  22. {
  23.     // message parameters
  24.     mail = new MailMessage();
  25.     mail.To.Add(new MailAddress(ToEmail, ToName));
  26.  
  27.     if (CCEmail != null && CCEmail != "")
  28.         mail.CC.Add(new MailAddress(CCEmail));
  29.  
  30.     mail.Subject = SubjectStr;
  31.     mail.Body = MessageStr;
  32.     mail.From = new MailAddress(MailFromEmail, MailFromName);
  33.  
  34.     mail.BodyEncoding = System.Text.Encoding.UTF8;
  35.     mail.SubjectEncoding = System.Text.Encoding.UTF8;
  36.     mail.IsBodyHtml = false;
  37.     mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  38.     mail.Priority = MailPriority.High;
  39.  
  40.     // SMTP parameters
  41.     smtp = new SmtpClient(MailServer);
  42.     smtp.Credentials = new NetworkCredential(MailFromEmail, MailPwd);
  43.     smtp.Port = 25;    // 25 or 587
  44.     // smtp.EnableSsl = true;  // required for gmail
  45.     smtp.Timeout = 20000;   // 20 seconds
  46.  
  47.     // attach ExportFile
  48.     System.Net.Mail.Attachment attachment;
  49.     attachment = new System.Net.Mail.Attachment(ExportFile);
  50.     mail.Attachments.Add(attachment);
  51.  
  52.     // send email
  53.     // smtp.SendAsync(mail, mail.Subject);
  54.     smtp.Send(mail);
  55.     Console.WriteLine("Mail sent!");
  56. }
  57. catch (Exception ex)
  58. {
  59.     Console.WriteLine("Mail error: " + ex.Message);
  60. }
GeSHi ©
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37

Video: Creating a Report with a Parameter

Postby finaquant » 09 Jan 2014, 23:09

How to Create a Report with a Parameter using ReportViewer in Visual Studio
http://www.youtube.com/watch?v=8o0p2mj5BbU
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37


Return to General

Who is online

Users browsing this forum: No registered users and 2 guests

cron