Question

I am having some problem when trying to set up an AJAX line chart in asp.net. What I am trying to do is I select a category from drop down list, then the line chart will display the sum of products sent out in every month by each category. Here is the code in presentation layer:

protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    string categoryName = ddlCategory.SelectedItem.ToString();
    string deliveryDate = "";
    decimal[] totalQuantity;

    List<ProductPacking> catSumList = new List<ProductPacking>();
    for (int count = 0; count < catSumList.Count; count++)
    {
        deliveryDate = catSumList[count].deliveryDate;
        totalQuantity = Convert.ToDecimal(catSumList[count].productQuantity);
    }
    lcCategory.Series.Add(new AjaxControlToolkit.LineChartSeries { Data = totalQuantity });
    lcCategory.CategoriesAxis = string.Join(",", deliveryDate);
    lcCategory.ChartTitle = string.Format("", deliveryDate);
    lcCategory.Visible = true;
}

And the code in Business Logic Layer:

public List<ProductPacking> getSumCategoryByMonth(string categoryName)
{
    List<ProductPacking> ft = new List<ProductPacking>();
    ft = prodPack.getSumCategoryByMonth(categoryName);
    return ft;
}

And the code in Data Access Layer:

public List<ProductPacking> getSumCategoryByMonth(string categoryName)
{
    List<ProductPacking> ft = new List<ProductPacking>();

    using (var connection = new SqlConnection(FoodBankDB.connectionString))
    {
        SqlCommand command = new SqlCommand("SELECT SUM(Convert(INT, ddi.productQuantity)) AS totalQuantity, pc.categoryName, d.deliveryDate FROM dbo.DistributionDistributedItems ddi " +
            " INNER JOIN dbo.ProductVariants pv ON ddi.productVariant = pv.id " +
            " INNER JOIN dbo.Products p ON pv.product = p.id " +
            " INNER JOIN dbo.ProductCategories pc ON p.productCategory = pc.id " +
            " INNER JOIN dbo.Distributions d ON ddi.distribution = d.id " +
            " WHERE categoryName = '" + categoryName + "'" + 
            " GROUP BY pc.categoryName, d.deliveryDate", connection);
        connection.Open();
        using (var dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                ft.Add(new ProductPacking(Convert.ToInt32(dr["totalQuantity"].ToString()), dr["deliveryDate"].ToString()));
            }
        }
    }
    return ft;
}

However, there is an error at the presentation layer around the

totalQuantity = Convert.ToDecimal(catSumList[count].productQuantity);

line. The error message is Cannot implicit convert type decimal to decimal[]. I referred from this website: Line Chart In Asp.Net

I wonder how to fix this according to my situation. Thanks in advance.

Was it helpful?

Solution

Move the declaration of totalQuantity after you get catSumList and change it to this:

List<ProductPacking> catSumList = new List<ProductPacking>();
catSumList = BLL.getSumCategoryByMonth("Some Category");
decimal[] totalQuantity = new decimal[catSumList.Count];

then enter the values of totalQuantity using count as the index in for block:

for (int count = 0; count < catSumList.Count; count++)
{
    deliveryDate = catSumList[count].deliveryDate;
    totalQuantity[count] = Convert.ToDecimal(catSumList[count].productQuantity);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top