Lecture 11 - Crystal Report Application
Lecture 11 - Crystal Report Application
• namespace WindowsFormsApp1
• {
• public partial class Form1 : Form
• {
• public Form1()
• {
• InitializeComponent();
• }
• public SqlConnection con;
• public SqlCommand cmd;
• private void Form1_Load(object sender, EventArgs e)
• {
• con = new SqlConnection();
• con.ConnectionString = "Data Source=DESKTOP-4TO03GQ;Initial
Catalog=esoft2022;Integrated Security=True";
• try
• {
• con.Open();
• SqlDataAdapter da = new SqlDataAdapter("select * from student", con);
• DataTable dt = new DataTable();
• da.Fill(dt);
• dataGridView1.DataSource = dt;
• con.Close();
• }catch(SqlException)
• {
• MessageBox.Show("Database error");
• }
• }
Step 3: Add another form (form2) to the current
project.
• Step 1: Go to solution
explorer and right click on
windows form app(might
differ depends on the project
name you have given)
• Step 2: After right click on
the project select add -> new
item.
• Step 3: select a new form.
• Step 4: After creating the
form select Crystal Report
View in from the tool box
and drop it on the form2
Step 4: Adding crystal report.
• Step 1: Go to solution explorer and right click on windows form app(might
differ depends on the project name you have given)
• Step 2: After right click on the project select add->new item.
• Step 3: Under report Select the “Crystal Report”.
• Step 4: After selecting the crystal report ,
• select blank report from the pop up form and click ok.
• Follow the below link to select the server and the database along with the table.
• Change the crystal report viewer: modifier from private to public.
Step 5: Write following code to the button given in the first form –
print button
• private void button1_Click(object sender, EventArgs e)
• {
• Form2 fr = new Form2();
• fr.Show();
• if(con.State != ConnectionState.Open)
• {
• con.Open();
• }
• SqlDataAdapter da = new SqlDataAdapter("select * from student", con);
• DataSet ds = new DataSet();
• da.Fill(ds);
• CrystalReport1 cr = new CrystalReport1();
• cr.SetDataSource(ds);
• fr.crystalReportViewer1.ReportSource = cr;
• fr.crystalReportViewer1.Refresh();
• con.Close();
• }
Step 5: View crystal report.
• Run the program
• The first form will appear with the data in the data grid view.
• Then click the print button to print the report.