Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a database like this:
T0 T1 T2
main_1 subcategory_1 Reason_1
main_2 subcategory_2 Reason_2
main_3 subcategory_3 Reason_3
main_4 subcategory_4 Reason_4
main_5 subcategory_2 Reason_4
main_1 subcategory_3 Reason_3
main_2 subcategory_2 Reason_2
main_1 subcategory_3 Reason_2
main_2 subcategory_3 Reason_3


I want to draw a pie chart for main_1 total subcategories are there. also draw a pie chart for Main_1 -> subcategory_1 for total reasons are there

What I have tried:

I had tried with
foreach (var item in Selected_TABLENAME)
                       {

                           if (item.Contains("All_Red_Events"))
                           {

                               foreach(var iteam1 in EventCategories)
                               {
                                   foreach(var iteam2 in SubEventCategories)
                                   {
                                           OleDbCommand cmd6 = new OleDbCommand();
                                           OleDbDataReader reader2 = null;
                                           cmd6 = new OleDbCommand("select  [T2] FROM " + item.ToString() + " WHERE ( [T0]='" + iteam1.ToString() + "'" +" and" + " [T1]='" + iteam2.ToString() + "')" + "", connection2);
                                           reader2 = cmd6.ExecuteReader();

                                           while (reader2.Read())
                                           {

                                               Subcategoriesreason.Add(reader2["T0"].ToString());
                                           }
                                   }
                               }
                            }
Posted
Updated 5-Jun-18 8:58am
Comments
Member 13812021 5-Jun-18 12:55pm    
There are many ways to this however it depends on what you are doing this in. Is it a Web Razor page, a win app, or something else. A pie chart is going to be based on a % or int value. What are you using to create the chart?
Member 13849478 6-Jun-18 2:09am    
I have MS-access database .I am fetching data from that database. Pie chart is for int value. and I am using this chart for showing the some data (like how many bugs are coming and in which area more like...)
Richard Deeming 6-Jun-18 11:02am    
cmd6 = new OleDbCommand("select  [T2] FROM " + item.ToString() + " WHERE ( [T0]='" + iteam1.ToString() + "'" +" and" + " [T1]='" + iteam2.ToString() + "')" + "", connection2);


Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Unfortunately, you can't use a parameter for the table name, so you should make sure that's validated extensively, and the value cannot be altered by the user.

The other two values should be passed as parameters:
using (OleDbCommand cmd6 = new OleDbCommand("select [T2] FROM [" + item + "] WHERE [T0] = ? and [T1] = ?", connection2))
{
    cmd6.Parameters.AddWithValue("T0", iteam1);
    cmd6.Parameters.AddWithValue("T1", iteam2);
    
    using (OleDbDataReader reader2 = cmd6.ExecuteReader())
    {
        while (reader2.Read())
        {
            Subcategoriesreason.Add(reader2["T0"].ToString());
        }
    }
}

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900