Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Crystal Reports » How to run Dynamic Crystal Report using Crystal Report Viewer in VS.NET 2005

How to run Dynamic Crystal Report using Crystal Report Viewer in VS.NET 2005

This article will explain about Dynamic Crystal Report using Crystal Report Viewer.

Author Rank:
Total page views :  65651
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor


MindCracker Certification

Some time we have multiple reports for the application and we need to load the report based on the user's selection. The following piece of code will explain how to achieve this.

using System; 

using System.Drawing; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 

using System.Data; 

using System.IO;

 

namespace DyanamicReport 

{ 

          /// <summary>

           /// Summary description for Form1. 

          /// </summary> 

          public class Form1 : System.Windows.Forms.Form 

          { 

                   private System.Windows.Forms.ComboBox CboReport; 

                   private System.Windows.Forms.Panel panel1; 

                   private CrystalDecisions.Windows.Forms.CrystalReportViewer cptReports; 

 

                   /// <summary> 

                   /// Required designer variable. 

                   /// </summary> 

                   private System.ComponentModel.Container components = null;  

                   public Form1() 

                   { 

                             // 

                             // Required for Windows Form Designer support 

                             // 

                             InitializeComponent(); 

                             // 

                             // TODO: Add any constructor code after InitializeComponent call 

                             // 

                   } 

                   /// <summary> 

                   /// Clean up any resources being used. 

                   /// </summary> 

                   protected override void Dispose( bool disposing ) 

                   { 

                             if( disposing ) 

                             { 

                                      if (components != null 

                                      { 

                                                components.Dispose(); 

                                      } 

                             } 

                             base.Dispose( disposing ); 

                   }

 

                   #region Windows Form Designer generated code 

                   /// <summary> 

                   /// Required method for Designer support - do not modify 

                   /// the contents of this method with the code editor. 

                   /// </summary> 

                   private void InitializeComponent() 

                   { 

                             this.CboReport = new System.Windows.Forms.ComboBox(); 

                             this.cptReports = new CrystalDecisions.Windows.Forms.CrystalReportViewer(); 

                             this.panel1 = new System.Windows.Forms.Panel(); 

                             this.panel1.SuspendLayout(); 

                             this.SuspendLayout(); 

                             //  

                             // CboReport 

                             //  

                             this.CboReport.Location = new System.Drawing.Point(0, 0); 

                             this.CboReport.Name = "CboReport"; 

                             this.CboReport.Size = new System.Drawing.Size(496, 21); 

                             this.CboReport.TabIndex = 0; 

                             this.CboReport.SelectedIndexChanged += new System.EventHandler
                             (
this.CboReport_SelectedIndexChanged_1); 

                             //  

                             // cptReports 

                             //  

                             this.cptReports.ActiveViewIndex = -1; 

                             this.cptReports.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 

                             this.cptReports.Location = new System.Drawing.Point(4, 3); 

                             this.cptReports.Name = "cptReports"; 

                             this.cptReports.SelectionFormula = ""; 

                             this.cptReports.Size = new System.Drawing.Size(717, 367); 

                             this.cptReports.TabIndex = 1; 

                             this.cptReports.ViewTimeSelectionFormula = ""; 

                             //  

                             // panel1 

                             //  

                             this.panel1.Controls.Add(this.cptReports); 

                             this.panel1.Location = new System.Drawing.Point(8, 40); 

                             this.panel1.Name = "panel1"; 

                             this.panel1.Size = new System.Drawing.Size(724, 383); 

                             this.panel1.TabIndex = 2; 

                             //  

                             // Form1 

                             //  

                             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 

                             this.ClientSize = new System.Drawing.Size(744, 422); 

                             this.Controls.Add(this.panel1); 

                             this.Controls.Add(this.CboReport); 

                             this.Name = "Form1"; 

                             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 

                             this.Text = "Special Reports"; 

                             this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 

                             this.Load += new System.EventHandler(this.Form1_Load); 

                             this.panel1.ResumeLayout(false); 

                             this.ResumeLayout(false); 

                   } 

                   #endregion 

                   /// <summary> 

                   /// The main entry point for the application. 

                   /// </summary> 

                   [STAThread] 

                   static void Main()  

                   { 

                             Application.Run(new Form1()); 

                   } 

                   private void Form1_Load(object sender, System.EventArgs e) 

                   { 

                             CboReport_Fill(); 

                   } 

                   private void CboReport_Fill() 

                   { 

                             //Create a temp table to hold the reports description and path 

                             DataTable dt = new DataTable (); 

                             dt.Columns.Add ("Description", System.Type.GetType("System.String")); 

                             dt.Columns.Add ("Path",System.Type.GetType("System.String")); 

                             //Create the first row of the temp table to tell users what to do 

                             string[] FirstRow = {" -- select a report to view --", "-1"}; 

                             dt.Rows.Add (FirstRow); 

                             //Pickup all the crystal reports in the Reports subdirectory of the program 

                             string[] fileList = Directory.GetFiles (Directory.GetCurrentDirectory() + @"\Reports",
                                                    "*.rpt");
 

                             foreach (string item in fileList) 

                             { 

                                      //add all the friendly report names into the temp table 

                                      int startPt = item.ToString().LastIndexOf(@"\Reports"); 

                                      string[] rowData = {item.Substring(startPt + @"\Reports".Length + 1, item.ToString

                                                                ().Length - (startPt + @"\Reports".Length + 1)),item}; 

                                      dt.Rows.Add (rowData); 

                             } 

                             //assign the temp table to the combo box 

                             CboReport.DataSource = dt; 

                             CboReport.DisplayMember = "Description"; 

                             CboReport.ValueMember = "Path"; 

                   } 

                   private void CboReport_SelectedIndexChanged_1(object sender, System.EventArgs e) 

                   { 

                             //identify if the first combo box item is not selected 

                             switch (CboReport.SelectedIndex > 0) 

                             { 

                                      //if another line in the combo box is selected then view the report  

                                      case true: 

                                                cptReports.ReportSource = CboReport.SelectedValue ; 

                                                cptReports.Zoom (25); 

                                                break; 

                                                //if the first line in the combo box is selected then clear the report 

                                      case false: 

                                                cptReports.ReportSource = null; 

                                                break; 

                             } 

                             cptReports.Zoom(100); 

                   } 

          } 

}

 

OutPut:-


Login to add your contents and source code to this article
 About the author
 
Sushmita Kumari
Sushmita kumari holds  Computer Science degree.She is Brain Bench and Microsoft certified professional. Presently she is working  as a software engineer. She has good hands on experience with .NET technologies. Other than .NET, she has worked with Microsoft Commerce Server, and Microsoft Content Management server.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
To display crystal report by siva On August 29, 2007
i want to display a receipt in a crystal report from four sqlserver table, any one having idea to send me thanks
Reply | Email | Delete | Modify | 
thanks by karuna On December 20, 2007
you are really great mam
Reply | Email | Delete | Modify | 
Hello by Amit On January 16, 2008
Hi Susmita i would like to know about Crystal report - how to generate crystal report at run time its depends upon the SQL Query. Amit
Reply | Email | Delete | Modify | 
Now only started by Fahmy On March 26, 2008
c# now in half way but i want to know how to create crystal report  by connecting  a table
Reply | Email | Delete | Modify | 
header and footer by karthik On May 8, 2009
hey i want to sent dynamically header and footer to the crystal report like user can adjust at runtime how can i achive that
Reply | Email | Delete | Modify | 
Re: header and footer by asm On September 7, 2009
please mail me the solution if u or any body found the solution of  ur problem at
feroz_tt@yahoo.co.in as i am facing the same problem
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2010  Mindcracker LLC. All Rights Reserved