Lahiru Mahagamage
Lahiru Mahagamage

Reputation: 97

Live charts data from database

 private void MapChart()
    {
        List<double> allValues = new List<double>();
        string day = null;

        if (CON.State == ConnectionState.Open)
        {
            CON.Close();
        }

        CON.ConnectionString = ConfigurationManager.ConnectionStrings["conDB"].ConnectionString;
        CON.Open();
        CMD = new SqlCommand("select * from tblWeeklyAudit", CON);
        RDR = CMD.ExecuteReader();
        while (RDR.Read())
        {
            allValues.Add(Convert.ToDouble(RDR["Defects"]));
            day = Convert.ToString(RDR["Day"]);
        }

        SeriesCollection = new SeriesCollection
        {
            new LineSeries
            {
                Values = new ChartValues<double>(allValues)
            }
        };

        Labels = new[] { "Day 1", "Day 2", "Day 3", "Day 4" };

        DataContext = this;
    }

I want to know how to get the values in day variable inside this.

Labels = new[] { "Day 1", "Day 2", "Day 3", "Day 4"};

Count of no. of days may vary. I'm doing this as my project for my degree.

Upvotes: 0

Views: 2043

Answers (1)

Shyju
Shyju

Reputation: 218702

Assuming you want the value of RDR["Day"] for every record inside the datareader to be added to the array/collection and the value of RDR["Day"] is something like Day1 or Day2 or DayN for each record, you can create a list of strings outside your loop and add to that inside your loop.

Similar to what you did in allValues list.

var dayLabels= new List<string>();
while (RDR.Read())
{
   allValues.Add(Convert.ToDouble(RDR["Defects"]));
   var d= RDR.GetString(RDR.GetOrdinal("Day"));     
   dayLabels.Add(d);
}

You can do most of the common operations of array (enumerating through it, getting an item from a specific index etc) with a list as well. If you absolutely want an array from this list, you can call the ToArray method on the list to get that

var myArray = dayLabels.ToArray();

Upvotes: 2

Related Questions