简体   繁体   中英

String was not recognized as a valid DateTime

In my Web Project I am Parsing the Date selected in the Date Picker and comparing it with the Date fetched from Database till Yesterday it was working fine but from today morning it throws the Format Exception what could be the Problem,

My code is,

  try
        {
            ReportDocument rpt = new ReportDocument();
            DateTime dt = DateTime.Parse(frmtxtdt.Text); // Exception Thrown here
            DateTime dt1 = DateTime.Parse(frmtxtdt.Text);
            DateTime date = DateTime.Parse(DateTime.Today.ToShortDateString());
            DateTime date1 = DateTime.Parse(DateTime.Today.ToShortDateString());
            string frtxt = String.Format("{0:MM-dd-yyyy}", dt);
            string totxt = String.Format("{0:MM-dd-yyyy}", dt1);
            DataSet ds = Namespace.SP.Storedprocedure(frtxt,totxt).GetDataSet();

            if (!IsPageRefresh)
           {
               if (ds.Tables[0].Rows.Count > 0)
               {
                   if(frtxt == ds.Tables[0].Rows[0]["Date"].ToString()
                   && totxt == ds.Tables[0].Rows[0]["Date"].ToString())
                   {

                          ds.Tables[0].TableName = "Passkeys";

                          ds.WriteXml(Server.MapPath("~/XML/Passkeys.xml"));
                          string filename = Server.MapPath("~/Upload/Pkey_rpt.rpt");

                          rpt.Load(filename);
                          rpt.SetDataSource(ds);

                          rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Passkeys - " + ds.Tables[0].Rows[0]["Date"].ToString());
                    }

            }
            else if(frmtxtdt.Text.Trim() !=null && totxtdt.Text.Trim()!=null)
            {
                if (frtxt == String.Format("{0:dd-MM-yyyy}", date)
                    && totxt == String.Format("{0:dd-MM-yyyy}", date1) 
                    && ds.Tables[0].Rows.Count == 0)
                {

                   ClientMessaging( "Pass Key(s) Not Yet Delivered for the Selected Date...");

                }
                else
                {

                    ClientMessaging( "There is No Schedule for the Selected date....");
                }

            }
         }

        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
        }

Today is the 13th of June 2013. It was probably working before because the day was 12 or less. You need to ensure the format of the date coming from the picker is the same as the format expected when parsing.

Currently, it looks like it's parsing the day as the month.

Be explicit on the CultureInfo, based on the date format you're expecting to parse

CultureInfo GBCultureInfo = new CultureInfo("en-GB"); // dd/MM/YYYY
// CultureInfo USCultureInfo = new CultureInfo("en-US"); // MM/dd/YYYY

dt = DateTime.Parse(frmtxtdt.Text,GBCultureInfo);

try like this,place code

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");//you can change to any country 
  ReportDocument rpt = new ReportDocument();

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