繁体   English   中英

转换为日期时间时无法将字符串识别为有效的日期时间

[英]String not recognized as a valid DateTime when converting to datetime

尝试插入一行数据时出现以下错误。 这说明该字符串未被识别为有效的日期时间。

C#

protected void saveExceptionAdd(object sender, System.EventArgs e)
  {
      //Default value secruity
      if (ddlTimeFromAdd.SelectedIndex == 0 || ddlTimeToAdd.SelectedIndex == 0)
      {
          lblAddExcept.Visible = true;
          lblAddExcept.Text = "Fields Required.";
          divExceptionAdd.Focus();
      }

      else
      {
          string EFTVFROM = txtDatefromAdd.Text.ToString() + ddlTimeFromAdd.SelectedValue.ToString();
          string EFTVTO = txtDatetoAdd.ToString() + ddlTimeToAdd.SelectedValue.ToString();

          DateTime eftvfromdt = Convert.ToDateTime(EFTVFROM);
          DateTime eftvtodt = Convert.ToDateTime(EFTVTO);

          //Update WeekDay restriction
          CDSSQLConnections.RunStoredProcedureWithNParams("connDataStore", "sp_AB_BULLETIN_EXCEPTION_INSERT",
          new Dictionary<string, object> { { "EFTVFROM", eftvfromdt }, { "EFTVTO", eftvtodt }, { "ABSTATUS", ddlStatus.SelectedValue }, { "LASTMODBY", CDSSecurity.CurrentUserID } });
          divExceptionAdd.Visible = false;
          lblAddExcept.Visible = false;
          repException.DataBind();
      }
  }

您可以告诉解析器应使用哪种日期时间格式来解析字符串

DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);

暂无
暂无

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

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