Student Result Application in Windows C# With Crystal Report

Introduction

This article provides a sample of a Windows application for student result details having a different subject in Visual Studio 2010. We also generate a Crystal Reports report in this application. We have a database table with Student Result Details. We also provide a search facility to find specific student results.

Note

We require Crystal Reports for Visual Studio 2010. To download Crystal Reports for Visual Studio 2010, exe or Zip, or for more information, click Here.

Step 1

Now we create a new project in Visual Studio 2010 using C#. From the file menu select "New Project". Here we use "Prg25_Stud_Crystal_Demo".

We add all the required controls to a Windows Forms form. We use Textboxes, Lables and Buttons.

We also add the Crystal Report Viewer for displaying the report of Student Results.

S1

Step 2

We add Crystal Reports to the application. For that go to Solution Explorer and click on "Add New Item" then add "Crystal Reports" from the given installed Templates or Items.

"Solution Explorer" ->"Add" -> "New Item" (Ctrl+Shift+A) -> "Crystal Reports".

S2

Step 3

After creating Crystal Reports we require a "Dataset" for Crystal Reports to display the student data in the report. For that go to the Solution Explorer and click on "Add New Item" then add a "DataSet" from the given installed Templates or Items.

"Solution Explorer" ->"Add" -> "New Item" (Ctrl+Shift+A) -> "DataSet".
To configure the Dataset we drag and drop the database in the DataSet configure wizard.

S3

After configuring the DataSet we must apply those datasets to Crystal Reports. This we do at runtime using C# code. We add database fields to the Crystal Report. We also generate Dynamic Fields (formula fields) to the Crystal Report, such as Per and Total.

S51

Step 4

After completing the design we need to do the coding. We add code in the Different Button Event to perform the Different task. Like for the "Submit" Button add the Student Records to the Database.

For searching purposes, on the "Search" Button we add code to find the specific Student Record.

Form1.cs

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
PRG25_STUD_CRYSTAL_DEMO
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbStudent.mdf;Integrated Security=True;User Instance=True");
        SqlCommand com;
        SqlDataAdapter adp = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            con.Open();
            com = new SqlCommand("insert into tbl_student values ("+txt_rollno.Text+",'"+txt_name.Text+"','"+txt_sub1.Text+"','"+txt_sub2.Text+"','"+txt_sub3.Text+"')",con);
            int x = com.ExecuteNonQuery();
            con.Close();
            if (x > 0)
            {
                ds.Tables[0].Rows.Clear();
                CrystalAddData();
                MessageBox.Show("Record Inserted");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            CrystalAddData();

        }

        public void CrystalAddData()
        {
            adp = new SqlDataAdapter("select * from tbl_student", con);
            adp.Fill(ds);
            ReportDocument rd = new ReportDocument();
            rd.Load(@"~\CrystalReport1.rpt");
            rd.SetDataSource(ds.Tables[0]);
            crystalReportViewer1.ReportSource = rd;  
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            if (txt_search .Text == "")
            {
                MessageBox.Show("Enter Roll No");
            }
            else
            {
                ds.Tables[0].Rows.Clear();
                adp = new SqlDataAdapter("select * from tbl_student where sno=" + txt_search .Text + "", con);
                adp.Fill(ds);
                ReportDocument rd = new ReportDocument();
                rd.Load(@"~\CrystalReport1.rpt");
                rd.SetDataSource(ds.Tables[0]);
                crystalReportViewer1.ReportSource = rd;
            }
        }

        private void btn_all_Click(object sender, EventArgs e)
        {
            ds.Tables[0].Rows.Clear();
            CrystalAddData();
        }
    }
}



Output

S5

Summary

Finally we have an application to generate student results and also generate the result Reports. We also export the report in a different format, like PDF, Word document and so on.

This application can help you to understand how to create a small application for reporting purposes.

I hope this article does help you.

Thank you.