Friday, January 29, 2016

C# Chart Control Tutorial In Urdu - Basics (Sales By Year Chart)


C# Code

using System;
using System.Data;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            SalesByYearChart.DataSource = GetData();
            SalesByYearChart.Series["SalesByYear"].XValueMember = "Year";
            SalesByYearChart.Series["SalesByYear"].YValueMembers = "Total";
        }

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

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

                    conn.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

                    dtData.Load(reader);
                }
            }

            return dtData;
        }
    }
}

Stored Procedure: [dbo].[usp_ChartData]

CREATE PROCEDURE [dbo].[usp_ChartData]
AS

 BEGIN

  SELECT   SUM(Total) AS 'Total'
    ,DATEPART(yyyy, [DateOfOrder]) AS 'Year'
  FROM [dbo].[Orders]
  GROUP BY DATEPART(yyyy, [DateOfOrder])

 END

No comments:

Post a Comment