简体   繁体   中英

String not recognized as a valid DateTime when converting to datetime

I am getting the following error when trying to insert a row of data. It is stating that the string was not recognized as a valid 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);

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.

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