Thursday, March 24, 2016

ASP.NET C# Tutorial In Urdu - How to use Chart Control In ASP.NET application


Code is below.

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

namespace Chart1
{
    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 = "Year";
            Chart1.Series["Series1"].YValueMembers = "Total";
            Chart1.DataBind();
        }

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

                    conn.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

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

                    return dtData;
                }
            }
        }
    }
}

Stored Procedure: usp_Chart1

CREATE PROCEDURE [dbo].[usp_Chart1]
AS
 BEGIN
  SELECT  SUM(Total) AS 'Total'
    ,DATEPART(yyyy, DateOfOrder) AS 'Year'
  FROM [dbo].[Orders]
  GROUP BY DATEPART(yyyy, DateOfOrder)
  HAVING DATEPART(yyyy, DateOfOrder) >= 1980 AND DATEPART(yyyy, DateOfOrder) <= 1985
 END
GO

No comments:

Post a Comment