Monday, February 8, 2016

C# Crystal Report Tutorial In Urdu - How to use Stored Procedure with Parameters In Crystal Report


Code here

Form - MainForm.cs code
using System;
using System.Windows.Forms;

namespace CrystalReportDemo5
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public enum RegistrationTypes
        {
            Online = 1,
            InPerson = 2,
            ByPhone = 3,
            Email = 4,
            ByPost = 5
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ShowReportButton_Click(object sender, EventArgs e)
        {
            ReportForm reportForm = new ReportForm();
            reportForm.RegistrationType = GetRegistrationType();
            reportForm.ShowDialog();
        }

        private int GetRegistrationType()
        {
            if (OnlineRadioButton.Checked)
            {
                return (int)RegistrationTypes.Online;
            }

            if (InPersonRadioButton.Checked)
            {
                return (int)RegistrationTypes.InPerson;
            }

            if (ByPhoneRadioButton.Checked)
            {
                return (int)RegistrationTypes.ByPhone;
            }

            if (EmailRadioButton.Checked)
            {
                return (int)RegistrationTypes.Email;
            }

            if (ByPostRadioButton.Checked)
            {
                return (int)RegistrationTypes.ByPost;
            }

            return 0;
        }
    }
}
Form - ReportForm.cs code
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

namespace CrystalReportDemo5
{
    public partial class ReportForm : Form
    {
        public ReportForm()
        {
            InitializeComponent();
        }

        public int RegistrationType { get; set; }

        private void ReportForm_Load(object sender, EventArgs e)
        {
            DataTable dtReportData = GetData();

            ShowReport(dtReportData);
        }

        private void ShowReport(DataTable dtReportData)
        {
            CrystalReptViewer.SelectionMode = CrystalDecisions.Windows.Forms.SelectionMode.None;

            ReportDocument rdoc = new ReportDocument();

            rdoc.Load(@"Reports\Report1.rpt");

            rdoc.SetDataSource(dtReportData);

            CrystalReptViewer.ReportSource = rdoc;
        }

        private DataTable GetData()
        {
            DataTable dtData = new DataTable();

           using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbx"].ConnectionString))
           {
               using (SqlCommand cmd = new SqlCommand("usp_ReportShowStudentsByRegistrationType", conn))
               {
                   cmd.CommandType = CommandType.StoredProcedure;

                   conn.Open();

                   cmd.Parameters.AddWithValue("@RegistrationType", this.RegistrationType);

                   SqlDataReader reader = cmd.ExecuteReader();

                   dtData.Load(reader);
               }
           }

           return dtData;
        }
    }
}

4 comments:

  1. Sir How to Display Total Month of Date in this Report. Like: Record Date is : 1-02-2016 To 31-02-2016.

    ReplyDelete
    Replies
    1. For this particular you don't need it because it is showing all data. But what you need to do is return start date and end date in the stored procedure as well. Then map it in the report.

      Delete
  2. Dear Sir Thank You for your quick Answer. But i want to get start date and End date. Tell me how to get in crystal Report using stored procedure .

    ReplyDelete
  3. I have issue when i run the crystal report i pass the parameter in crystal report viewer but after the that the popup dialog is show.. and they want to Enter the value. how can i avoid this..???

    Sir kindly help me ho to avoid this dialog at after Execute the crystal report at run time.
    I want to pass my parameters directly not show this dialog kindly tell me...???????

    ReplyDelete