how to create rdlc report in c# windows application

Advertisements
how to create rdlc report in c# windows application


See my Previous Article for that Form1.I am designing my screen having,Combo Box, Two DateTimePicker and Button for Generating report.
How to Page number in rdlc reports
How to add no data found message in rdlc report
My Out Put looks like this,By default I will display All information.
If we select the combo box options and click on Generate Button,It will Show the Out Put Based on My Requirement




I Designed the Report Form UI looks like this.

                            

I  Design  database looks like this.



Now I am creating the stored procedure to generate reports based upon date selection.
See my Previous Article where we can select existing stored procedure.

USE [chinnu]
GO

/****** Object:  StoredProcedure [dbo].[SP_GenerateReport]    Script Date: 08/05/2013 04:15:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create procedure [dbo].[SP_GenerateReport]

(
@StartDate DateTime
,@EndDate DateTime  
)                                
as
BEGIN

 SELECT EmpId,Convert(varchar,CreatedDate,101) As CreatedDate ,EmpName,EmpAddress
    FROM Employee
    WHERE  Convert(varchar,CreatedDate,101) between  @StartDate and  @EndDate    
    
  
END
GO




Finally my solution looks like this.






   Now write the code in 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;

namespace RdlcReports
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ddlperiod.Items.Add("All");
            ddlperiod.Items.Add("Today");
            ddlperiod.Items.Add("Yesterday");
            ddlperiod.Text = "All";
            // TODO: This line of code loads data into the 'DataSet1.SP_GenerateReport' table. You can move, or remove it, as needed.
            //this.SP_GenerateReportTableAdapter.Fill(this.DataSet1.SP_GenerateReport);

            this.SP_GenerateReportBindingSource.DataSource = ReportData();
            ReportData();
            this.reportViewer1.RefreshReport();
        }

        public static DataTable ReportData()
        {
            DataTable DatTab = new DataTable();
            try
            {

                SqlConnection con = new SqlConnection("Data Source=DotNetSharePoint;Initial Catalog=chinnu;User ID=sa;Password = Admin1234;" );
          
                con.Open();
                if (true)
                {
                    con.Close();
                    SqlCommand cmd = new SqlCommand("SELECT EmpId,Convert(varchar,CreatedDate,101) As CreatedDate ,EmpName,EmpAddress FROM Employee", con);
    
                    //cmd.CommandType = CommandType.StoredProcedure;

                    SqlDataAdapter adap = new SqlDataAdapter(cmd);
                    adap.Fill(DatTab);

                }

                else
                {



                }
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return DatTab;
        }

        private void btnReprt_Click(object sender, EventArgs e)
        {
           string  Fdate = ddlstartdate.Text;
           string Ldate = ddlenddate.Text;


             DataTable DatTab = new DataTable();
            SqlConnection con = new SqlConnection("Data Source=DotNetSharePoint;Initial Catalog=chinnu;User ID=sa;Password = Admin1234;");
            con.Open();
            if (ddlperiod.Text == "All")
            {
                SqlCommand cmd = new SqlCommand("SELECT EmpId,Convert(varchar,CreatedDate,101) As CreatedDate ,EmpName,EmpAddress FROM Employee", con);
               

                SqlDataAdapter adap = new SqlDataAdapter(cmd);
                adap.Fill(DatTab);
            }

            else
            {

                using (SqlCommand cmd = new SqlCommand("SP_GenerateReport", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("StartDate", SqlDbType.DateTime)).Value = Convert.ToDateTime(Fdate).ToShortDateString();
                    cmd.Parameters.Add(new SqlParameter("EndDate", SqlDbType.DateTime)).Value = Convert.ToDateTime(Ldate).ToShortDateString();
                    cmd.ExecuteNonQuery();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(DatTab);
                }
            }

            con.Close();
            this.SP_GenerateReportBindingSource.DataSource = DatTab;
            this.reportViewer1.RefreshReport();

        }

        private void ddlperiod_SelectedIndexChanged(object sender, EventArgs e)
        {

            var toDate = DateTime.Now;
            var fromDate = DateTime.Today;

            switch (ddlperiod.Text)
            {
                case "Today": fromDate = toDate; break;
                case "Yesterday": fromDate = toDate.AddDays(-1); toDate = toDate.AddDays(-1); break;
            
                default: break;
            }

            ddlstartdate.Text = fromDate.ToShortDateString();
            ddlenddate.Text = toDate.ToShortDateString();

        }
    }
}





Now Add AppConfig for DataBase Connection.

        
    

After Completing the Steps,Press F5.


OutPut:



Now Select Today Date Click on Generate Report,It will shows today's Information





Advertisements
SHARE

lakshmitulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

4 comments:

  1. Hi, e i am newbi and i do not know what SP_GenerateReportBindingSource, whould you please tell me what controls is that, thnks a lot

    ReplyDelete
  2. SP_GenerateReportBindingSource same issue

    ReplyDelete
  3. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. UC mini

    ReplyDelete