Tuesday, February 2, 2016

C# Chart Control Tutorial In Urdu - How to Show Percentage data In Pie Chart


Code here

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

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

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

        private void LoadData()
        {
            chart1.DataSource = GetData();
            chart1.Series["Series1"].XValueMember = "Gender";
            chart1.Series["Series1"].YValueMembers = "Total";
        }

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

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

                    conn.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

                    dtChartData.Load(reader);
                }
            }

            return dtChartData;
        }
    }
}

Stored Procedure: usp_ChartData6

CREATE PROCEDURE usp_ChartData6
AS
 BEGIN

  SELECT COUNT(StudentId) * 1.0 /(SELECT COUNT(StudentId) FROM [dbo].[Students]) AS 'Total'
    ,Gender AS 'Gender'
  FROM [dbo].[Students]
  GROUP BY Gender

 END

No comments:

Post a Comment