I am trying to read the Date and Amount_Fuel from SQL to plot a bar graph of fuel consumption at weekly interval.The graph is supposed to plot Amount_Fuel against week(s).However,I am getting the 'Specified cast is not valid' error when reading Date with date_time/date data type and Amount_Fuel with int data type from a Fuel_Attended table in the SQL SERVER 2012. Here is the current code giving the highlighted error`
private void Load_BTN_Click(object sender, EventArgs e)
{
try
{
string selectQuery = "Select sum(Fuel_Amount), DATEADD(week, DATEDIFF(week, '1950-01-01', Date), '1950-01-01') AS WeekNumber from Fuel_Attendend group by DATEADD(week, DATEDIFF(week, '1950-01-01', Date), '1950-01-01')";
cmd = new SqlCommand(selectQuery, connection);
try
{
connection.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
this.chart1.Series["Date"].Points.AddXY(dr.GetDateTime(0), dr.GetInt32(1));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
connection.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
this.chart1.Series["Date"].Points.AddXY(dr.GetInt32(1), dr.GetDateTime(0));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.