简体   繁体   English

格式异常:字符串未被识别为有效的DateTime

[英]Format Exception:String was not recognized as a valid DateTime

hi I have method for getting the values form database and representing those values in chart controls.. 嗨,我有从数据库中获取值并在图表控件中表示这些值的方法。

and this is the method... 这是方法......

      public static void Hourlyattendence(System.DateTime startdate, System.DateTime enddate, string StartHour, string EndHour,
                                           out string[] Hours, out int[] Accepted, out int[] Refused)
     {
         int hours = 1 + int.Parse(EndHour) - int.Parse(StartHour);

         Hours = new string[hours];
         Accepted = new int[hours];
         Refused = new int[hours];

         int result = 0;

         for (int i = 0; i < hours; i++)
         {
             Accepted[i] = 0;
             Refused[i] = 0;
             Hours[i] = string.Format("{0:00}", int.Parse(StartHour) + i);
         }

         const string sql = @"SELECT COUNT('x') AS numVisits, visit_Status as Status, SUBSTRING(visit_Time,1,2) as visitHour
                              FROM visits
                              WHERE visit_Date BETWEEN @startdate AND @enddate
                              AND SUBSTRING(visit_Time,1,2) between @StartHour and @EndHour
                              GROUP BY SUBSTRING(visit_Time,1,2), visit_Status";

         var hourstable = new DataTable();

         using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
         {
             conn.Open();

             var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);

             var ds = new DataSet();
             var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter.Direction = ParameterDirection.Input;
             parameter.Value = startdate;
             cmd.Parameters.Add(parameter);

             var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter2.Direction = ParameterDirection.Input;
             parameter2.Value = enddate;
             cmd.Parameters.Add(parameter2);

             var parameter3 = new MySql.Data.MySqlClient.MySqlParameter("@StartHour", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter3.Direction = ParameterDirection.Input;
             parameter3.Value = StartHour;
             cmd.Parameters.Add(parameter3);

             var parameter4 = new MySql.Data.MySqlClient.MySqlParameter("@EndHour", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter4.Direction = ParameterDirection.Input;
             parameter4.Value = EndHour;
             cmd.Parameters.Add(parameter4);

             var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);

             da.Fill(ds);
             try
             {
                 hourstable = ds.Tables[0];

             }
             catch
             {
                 hourstable = null;
             }
         }
         if (hourstable != null)
         {
             for (int i = 0; i < hourstable.Rows.Count; i++)
             {
                 if (int.TryParse(hourstable.Rows[i]["numVisits"].ToString(), out result) && int.TryParse(hourstable.Rows[i]["visitHour"].ToString(), out hours))
                 {
                     hours -= int.Parse(StartHour);
                     if (hourstable.Rows[i]["Status"].ToString().ToUpper() == "ACCEPTED")
                     {
                         Accepted[hours] = result;
                     }
                     else
                     {
                         Refused[hours] = result;
                     }
                 }

             }

         }

     }

and i am calling above method in the below code ...and this is where i am representing the data in chart controls.... 我在下面的代码中调用上面的方法...这就是我在图表控件中表示数据的地方....

    private void KpiHourlAttendenceForm_Load(object sender, EventArgs e)
    {
        Drawkpihourlyattendence(dtpStartDate.Value, dtpEnddate.Value);
    }

    public void Drawkpihourlyattendence(DateTime startdate, DateTime enddate)
    {
        try
        {
            KpiHourlyattaendencechart.Series.Clear();

            Series acceptedSeries = KpiHourlyattaendencechart.Series.Add("Accepted");
            Series rejectedSeries = KpiHourlyattaendencechart.Series.Add("Refused");

            string[] xValues;
            int[] yValues;
            int[] yValues2;

            KpiData.Hourlyattendence(startdate, enddate, "06", "22", out xValues, out yValues, out yValues2);

            //blah
           //blah
           ///blah
       }
       catch(FormatException e)
      {
            Console.WriteLine(e.Message);

       }    

got the exception : format Exception String was not recognized as a valid DateTime. 得到异常:格式异常字符串未被识别为有效的DateTime。

would any one pls help on this..... 任何人都会对此有所帮助.....

Many Thanks in advance... 提前谢谢了...

将开始/结束时间更改为整数SQL参数类型,并将SUBSTRING(visit_Time,1,2)转换为您在BETWEEN @StartHour和@EndHour中执行的行中的整数

StartHour and EndHour are not DateTime . StartHourEndHour不是DateTime

Additionally, as @Ryan Wright pointed out, your between filter isn't going to work as expected with strings. 此外,由于@Ryan赖特指出,你between过滤器不会与弦预期工作。

我认为您必须更改@StartHour@EndHour的参数类型,因为它们是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM