Initial chamber
Step 1: Open Visual Studio 2010, Go to File, New, Projects and under Visual C# select Windows.
You can change the name of the project and browse your project to different location too. Then press – OK.
Step 2: In Solution Explorer you will get your Project, Add Service Based Database. By going to your project right click and Add New Item, then go to Service-based Database.
Database chamber
Step 3: Go to your database Database.mdf, we will create a table - tbl_Data. Go to the database.mdf -Table and add new table. Design your table like the following:
Table - tbl_data (Don’t forget to make ID - Identity Specification - Yes)
Insert some data inside your table by going to your table - tbl_data, right click and show tbl_data - Add data in the table.
Stored Procedure - sp_select:
Double click on the dataset that resides under App_code_Folder. Here's the image:
When you double click the dataset you get the blank area, there you have to do something like the following:
Right click on the blank area - Add DataTable.
Change the name of table from DataTable1 to Newtbl_data, there you have to add 4 columns for ProductID, ProductName, Quantity, UnitPrice as we had taken in tbl_data, so add three Columns. You will get something like the following image.
After this process you have to go the crystalrpt_demo - Right Click, Add New Item and find Crystal Report.rpt file.
When you add this a new Crystal Report Gallery opens and there you have to do the following:
You got something like this:
When your crystal report opens like the above image, you can find in the left or right dock, the Field Explorer is visible.
Right Click on Field Explorer, Database Expert and create new connection. You have to find the table that we had made in Dataset i.e - Newtbl_data.
Once you got your table, add that table to the right hand side pane using “ >>” button and Press OK. Here's the image:
You will get you Newtbl_data in Field Explorer like this.
Here you have to add the ProductID, ProductName, Quantity and Unit Price from Database Fields to the Crystal Report - Section 3 Page Details. You can drag these fields to the Crystal Report Page Details.
Design chamber
Step 4: Now open your Form1.cs file, where we create our design for Crystal Report Binding
We will drag Crystal Report Viewer from tool box to Form1.cs. Your form will look like the following:
Code chamber
Step 5: Open your Form1.cs and write some code so that our application starts working.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using CrystalDecisions.CrystalReports.Engine;
- namespace WindowsFormsApplication5
- {
- public partial class Form1: Form
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
- ReportDocument rprt = new ReportDocument();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- rprt.Load(@"C:\Users\Nilesh\Documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\CrystalReport1.rpt");
- SqlCommand cmd = new SqlCommand("sp_select", con);
- cmd.CommandType = CommandType.StoredProcedure;
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds, "Newtbl_data");
- rprt.SetDataSource(ds);
- crystalReportViewer1.ReportSource = rprt;
- }
- }
- }
Output chamber Hope you liked this. Thank you for reading. Have a good day.