0

Tried the codes on the net concerning the error

You can only set 1 Y values for this data point. Parameter name: yValue

try
{
    con.Open();
    cmd = new OleDbCommand("SELECT Sum(GrandTotal) AS SumOfGrandTotal,InvoiceDate FROM Invoice GROUP BY InvoiceDate HAVING (InvoiceDate between #" + dateTimePicker1.Value + "# and #" + dateTimePicker2.Value + "#);", con);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataView source = new DataView(ds.Tables[0]);
    chart1.DataSource = source;
    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    while (dr.Read() == true)
    {
        chart1.Series[0].Points.AddXY(dr["InvoiceDate"].ToString());//,dr["GrandTotal"].ToString());
        //chart1.Series[0].YValuesPerPoint = 2;
    }
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

I am using . This code is for Chart Column. I want to get the date on database and get their sum of grandtotal.

Abhishek
  • 2,925
  • 4
  • 34
  • 59
  • Only some ChartTypes support or need more than one Y-Value. What charttype do you use? Which line gives the error? Where is the `yValue` that is mentionend in th error msg? Also see [here](http://stackoverflow.com/questions/14347454/how-to-bind-data-to-chart-in-winforms-and-refresh-chart) and [here](https://msdn.microsoft.com/de-de/library/dd456766.aspx) – TaW Sep 28 '15 at 15:45
  • its column. on my database im getting 2 input of date and 1 sum of grand total. – anjohnnette Sep 29 '15 at 11:50
  • Btw: the commented out part of the AddXY sets the Y-Values to strings. This is nonsense as only numbers (or DateTimes (which will still be converted to double) make any sense for the y-values. The same applies to the X-values, although here it is less strict: If you add those a strings the DatAPoints will be added as if you had set them to 0,1,2.. but internally they will still be numbers, namely all will be zero.. The stringse will show up in the labels, as if you had done it right..but you can't use the X-values. – TaW Sep 29 '15 at 15:21
  • Also: If you want to use the `YValuesPerPoint` as a workaround (but you really should go to the bottom of the problem) then you need to move it before the AddXY and also out of the loop! And you should look into your DataSource to see how many columns it actually has.. – TaW Sep 29 '15 at 15:30

1 Answers1

0

Try to change

chart1.Series[0].Points.AddXY(dr["InvoiceDate"].ToString());//,dr["GrandTotal"].ToString());

To

chart1.Series[0].Points.AddXY(dr["InvoiceDate"].ToString(), (double)dr["SumOfGrandTotal"]);
asdev
  • 943
  • 6
  • 9
  • index out of range exception `GrandTotal` i think my query only gets the date and gets the sum of all grandtotal on database. – anjohnnette Sep 29 '15 at 14:48
  • Sorry I used `(double)dr["GrandTotal"])` where I should have used `(double)dr["SumOfGrandTotal"])` because of the SUM-Alias. I edited the code – asdev Sep 29 '15 at 16:21
  • The query should be all right. Otherwise, ´dr.Read()´ would return false – asdev Sep 29 '15 at 16:22
  • its working now but why does the date have a time also? `9/30/15 12:00:00 AM` – anjohnnette Sep 29 '15 at 16:46
  • Because this is the standard format for date and time. If you want another date display, you must format the datestring: see https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx – asdev Sep 29 '15 at 17:08
  • can the `12:00:00 AM` be remove? im using long date format. – anjohnnette Sep 30 '15 at 05:20
  • Yes, see here for example: http://stackoverflow.com/questions/501460/format-date-in-c-sharp – asdev Sep 30 '15 at 08:26
  • so if i put it on my form it will change how the graph gets the date from database? sorry currently at school cant change my code. – anjohnnette Sep 30 '15 at 09:22