Hello Mudassar,
THanks for your reply.
After reinstalling the Northwind Database, the problem is fixed.
Now another problem is the chart cannot display the lines properly.
Please refer to the image below:
https://plus.google.com/photos/107806971633639649167/albums/5855169931916874865/5855169935280495474?authkey=COSDzJKh4Za53gE
.cs content is listed below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select distinct shipcountry from orders";
DataTable dt = GetData(query);
ddlCountry1.DataSource = dt;
ddlCountry1.DataTextField = "shipcountry";
ddlCountry1.DataValueField = "shipcountry";
ddlCountry1.DataBind();
ddlCountry2.DataSource = dt;
ddlCountry2.DataTextField = "shipcountry";
ddlCountry2.DataValueField = "shipcountry";
ddlCountry2.DataBind();
ddlCountry2.Items[1].Selected = true;
}
}
protected void Compare(object sender, EventArgs e)
{
string query = string.Format("select datepart(year, orderdate) Year, count(datepart(year, orderdate)) TotalOrders from orders where shipcountry = '{0}' group by datepart(year, orderdate)", ddlCountry1.SelectedItem.Value);
DataTable dt = GetData(query);
string[] x = new string[dt.Rows.Count];
decimal[] y = new decimal[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
LineChart1.Series.Add(new AjaxControlToolkit.LineChartSeries { Name = ddlCountry1.SelectedItem.Value, Data = y });
query = string.Format("select datepart(year, orderdate) Year, count(datepart(year, orderdate)) TotalOrders from orders where shipcountry = '{0}' group by datepart(year, orderdate)", ddlCountry2.SelectedItem.Value);
dt = GetData(query);
y = new decimal[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
LineChart1.Series.Add(new AjaxControlToolkit.LineChartSeries { Name = ddlCountry2.SelectedItem.Value, Data = y });
LineChart1.CategoriesAxis = string.Join(",", x);
LineChart1.ChartTitle = string.Format("{0} and {1} Order Distribution", ddlCountry1.SelectedItem.Value, ddlCountry2.SelectedItem.Value);
LineChart1.Visible = true;
}
private static DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
}
Thanks very much for your time!