Question

I am trying to return the results to a chart and graph it. My database data returned is a datetime and a float which should work since they are both objects. The error I get is

cannot convert from string to int

for both arguments in the AddXY method.

string dateSelected = monthCalendarAdv1.Value.ToShortDateString();
dataConnection.Open();

SqlCommand dataCommand4 = new SqlCommand("SpTimeSeriesTotalParity", dataConnection);
dataCommand4.Connection = dataConnection;
dataCommand4.CommandType = CommandType.StoredProcedure;
dataCommand4.Parameters.Add(new SqlParameter("@ValDate", dateSelected));
dataCommand4.Parameters.Add(new SqlParameter("@Acct", cmbSelectAccno.Text));

The following code was similar and was working for the first argument without any issue.

this.chart1.Series["$Parity"].Points.AddXY("Item1",1);
this.chart1.Series["$Parity"].Points.AddXY("Item2", 2); 
this.chart1.Series["$Parity"].Points.AddXY("Item3", 3); 
this.chart1.Series["$Parity"].Points.AddXY("Item4", 4); 
this.chart1.Series["$Parity"].Points.AddXY("Item5", 5); 

try
{
    dataConnection.Open();
    using (SqlDataReader myReader = dataCommand4.ExecuteReader())
                    // myReader = dataConnection.ExecuteReader();
       while (myReader.Read())
       {
           this.chart1.Series["$Parity"].Points.AddXY
              (myReader.GetDateTime("ValuationDate"), myReader.GetDouble("SumParity"));
       }
}
Was it helpful?

Solution

The problem here is not your addxy method, but the Command reader getter methods.

Both expect the 0-Based index of the column and not it's name.

Change your code to something like this:

this.chart1.Series ["$Parity"].Points.AddXY (myReader.GetDateTime(2), myReader.GetDouble(3));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top