D B
D B

Reputation: 306

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();
      }
  }

Upvotes: 0

Views: 90

Answers (1)

Taha Paksu
Taha Paksu

Reputation: 15616

You can tell the parser which datetime format it should use to parse the string

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

Upvotes: 3

Related Questions