Saturday, January 30, 2016

C# Chart Control Tutorial In Urdu - Load Data Dynamically in Chart Control



C# Code

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            FromComboBox.Text = "2012";
            ToComboBox.Text = "2015";

            LoadData();
        }

        private void LoadData()
        {
            SalesByYearChart.DataSource = GetData();
            SalesByYearChart.Series["Series1"].Points.Clear();
            SalesByYearChart.Series["Series1"].XValueMember = "Year";
            SalesByYearChart.Series["Series1"].YValueMembers = "Total";
        }

        // DataSet, Objects, Array, Collection, Generics, DataTable
        private DataTable GetData()
        {
            DataTable dtChartData = new DataTable();

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

                    conn.Open();

                    cmd.Parameters.AddWithValue("@FromYear", FromComboBox.Text);
                    cmd.Parameters.AddWithValue("@ToYear", ToComboBox.Text);

                    SqlDataReader reader = cmd.ExecuteReader();

                    dtChartData.Load(reader);
                }
            }

            return dtChartData;
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}

Stored Procedure: [dbo].[usp_ChartData2]

CREATE PROCEDURE [dbo].[usp_ChartData2]
(
  @FromYear NVARCHAR(50)
 ,@ToYear NVARCHAR(50)
)
AS

 BEGIN

  SELECT    SUM([Total]) AS 'Total'
     ,DATEPART(yyyy, [DateOfOrder]) AS 'Year'
  FROM  [dbo].[Orders]
  GROUP BY DATEPART(yyyy, [DateOfOrder])
  HAVING  DATEPART(yyyy, [DateOfOrder]) >= @FromYear AND DATEPART(yyyy, [DateOfOrder])  <= @ToYear

 END

2 comments:

  1. how can download this video????

    ReplyDelete
    Replies
    1. You can download it from YouTube.com,using youtube downloader.

      Delete