Saturday, March 26, 2016

ASP.NET C# Tutorial In Urdu - How to show Percentage (%) data In 3D Pie Chart


Code is below.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Chart3
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

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

        // Object, Array, Collection, Generic, DataSet, DataTable
        private DataTable GetData()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbx"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("usp_Chart3", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    conn.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

                    DataTable dtData = new DataTable();
                    dtData.Load(reader);

                    return dtData;
                }
            }
        }
    }
}

Stored Procedure: usp_Chart3

CREATE PROCEDURE usp_Chart3
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