String was not recognized as a valid DateTime. - InfoPath Dev Sign in | Join | Help in Code InfoPath (Entire Site) InfoPath Dev InfoPath Dev is dedicated to bringing you the information and tools you need to be successful in your Microsoft Office InfoPath development projects. Home Blogs Forums Photos Downloads InfoPath Dev » InfoPath » Code » String was not recognized as a valid DateTime. String was not recognized as a valid DateTime. Last post 01-09-2009 09:55 AM by Hilary Stoupa. 15 replies. Page 1 of 2 (16 items) 1 2 Next > Sort Posts: Oldest to newest Newest to oldest Previous Next 01-09-2009 05:01 AM BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 String was not recognized as a valid DateTime. Reply Contact Hello Everyone. I am creating the ability to set an appointment in someone's calendar and use values in the form to set the attributes of the appoinement. I am getting stuck trying to convert two fields to DateTime. I get this error: System.FormatException was unhandled by user code Message="String was not recognized as a valid DateTime." Source="mscorlib" StackTrace: at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider) at System.Convert.ToDateTime(String value) at Customer_Care___no_code.FormCode.CTRL263_8_Clicked(Object sender, ClickedEventArgs e) in C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs:line 150 at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent) at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent) Here is the bit of code that is generating the error. //Get Values for Date/Time //Start DateXPathNavigator StartDateField = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:WorkOrders/@DiagnoseDate", NamespaceManager); string StartDateSeed = StartDateField.Value; //Start TimeXPathNavigator StartTimeField = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:WorkOrders/@DiagnoseTime", NamespaceManager); string StartTimeSeed = StartTimeField.Value; // Set the start date myAppointment.Start = Convert.ToDateTime(StartDateSeed+StartTimeSeed); <====== the error happens here. Any help would be greatly appreciated. Fishman 01-09-2009 05:18 AM In reply to swathip Joined on 10-30-2007 India Posts 487 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Hi Guru, Instead of myAppointment.Start = Convert.ToDateTime(StartDateSeed+StartTimeSeed); , use the following code line. myAppointment = Convert.ToDateTime(StartDateSeed+ "T" +StartTimeSeed); Swathipwww.ggktech.com 01-09-2009 05:28 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Good Morning Swathip. Thank you for your assistance, but now I am getting this error: Cannot implicitly convert type 'System.DateTime' to 'Microsoft.Office.Interop.Outlook._AppointmentItem' C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs 150 29 Customer Care - no code Fishman 01-09-2009 05:30 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Additionally, I have found that if I omit just the +StartTimeSeed I get the appointment with the correct date, but a time of 12 am - I assume because the time element is missing. Fishman 01-09-2009 05:35 AM In reply to swathip Joined on 10-30-2007 India Posts 487 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Actually what is the data type of myAppointment? Swathipwww.ggktech.com 01-09-2009 05:41 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact myAppointment is referrming to the AppointmentItem Object for creating the appointment in the calendar. Here is the rest of the code - maybe that will help: //Add a reference to Microsoft Outlook 12.0 Object Library. Create new instance of Microsoft.Office.Interop.Outlook.Application object: Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); // Get the namespaceMicrosoft.Office.Interop.Outlook.NameSpace ns = outlookApp.GetNamespace("MAPI"); // Create a receipient object that is going to be the organizer of the appointmentMicrosoft.Office.Interop.Outlook.Recipient myOrganizer = ns.CreateRecipient("apcarroll"); myOrganizer.Resolve(); // Open the shared calendar for the receipient Microsoft.Office.Interop.Outlook.MAPIFolder sharedCalendar = ns.GetSharedDefaultFolder(myOrganizer, Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); //Create an instance of AppointmentItem object and set the properties: Microsoft.Office.Interop.Outlook._AppointmentItem myAppointment = (Microsoft.Office.Interop.Outlook._AppointmentItem)sharedCalendar.Items.Add(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem); //Get values for appointment fields //SubjectXPathNavigator SubjectField1 = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/@WWALPH", NamespaceManager); string SubjectSeed1 = SubjectField1.Value;XPathNavigator SubjectField2 = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:Lots/@community_description", NamespaceManager); string SubjectSeed2 = SubjectField2.Value;XPathNavigator SubjectField3 = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:Lots/@szLotNumber_201", NamespaceManager); string SubjectSeed3 = SubjectField3.Value; //LocationXPathNavigator LocationField1 = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:Lots/@szLotAddress_201", NamespaceManager); string LocationSeed1 = LocationField1.Value;XPathNavigator LocationField2 = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/@ConcatPh1", NamespaceManager); string LocationSeed2 = LocationField2.Value; //BodyXPathNavigator BodyField1 = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:WorkOrders/d:WorkOrderConcern/d:WorkOrderConcernInfo/@UpdatedInfo", NamespaceManager);string BodySeed1 = BodyField1.Value; myAppointment.Subject = (SubjectSeed1+SubjectSeed2+SubjectSeed3); myAppointment.Body = BodySeed1; myAppointment.Location = (LocationSeed1 + LocationSeed2); //Get Values for Date/Time //Start DateXPathNavigator StartDateField = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:WorkOrders/@DiagnoseDate", NamespaceManager); string StartDateSeed = StartDateField.Value; //Start TimeXPathNavigator StartTimeField = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Buyers/d:WorkOrders/@DiagnoseTime", NamespaceManager); string StartTimeSeed = StartTimeField.Value; // Set the start datemyAppointment.Start = Convert.ToDateTime(StartDateSeed StartTimeSeed); // End date myAppointment.Duration = 60; // Set the reminder 15 minutes before startmyAppointment.ReminderSet = true; myAppointment.ReminderMinutesBeforeStart = 15; //Setting the sound file for a reminder: myAppointment.ReminderPlaySound = true; //Setting the importance: //use OlImportance enum to set the importance to low, medium or highmyAppointment.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;/* OlBusyStatus is enum with following values: olBusy olFree olOutOfOffice olTentative */myAppointment.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy; //Finally, save the appointment: // Save the appointment myAppointment.Save(); Fishman 01-09-2009 05:54 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact I think I may have figured it out. the two fields that I am using to create the myAppointment.Start are both date/time fields. I have formatted them to display just the date in one field and just the time in the other field, but i think that IP is putting the date and the time in both fields, but just not displaying it. Do you know how to strip the time off the first field and the date off the second field? Fishman 01-09-2009 06:02 AM In reply to swathip Joined on 10-30-2007 India Posts 487 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Hi, Change the first field data type to Date instead of Date and Time. Similarly the second field data type to time instead of Date and Time. Swathipwww.ggktech.com 01-09-2009 06:12 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact That was the first thing that I thought of, but there are complications. I am getting the data from/storing to a SQL DB as well as publishing the form to a SharePoint forms library. The two fields in question are promoted to columns in the forms library and if I don't have them formatted as DateTime, SP tries to stick the missing element in there for me so instead of the date I get 2009-01-09T00:00:00 and instead of the time I get 1899-1-1T09:05:30. It is odd I know and probably something to do with our SP configuration, but that is something that I do not have any control over, so here I am. Fishman 01-09-2009 06:26 AM In reply to swathip Joined on 10-30-2007 India Posts 487 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Hi, You can get the only date value from the date and time field using the following. StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")) And the time from the date and time field using StartTimeSeed.Substring(StartTimeSeed.IndexOf("T")+1, StartTimeSeed.Length- StartTimeSeed.Substring(0, StartTimeSeed.IndexOf("T")).Length-1) Swathipwww.ggktech.com 01-09-2009 07:14 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Sorry to be such a pain, but I am afraid that I do not know much about writing code. I am now getting this error: Length cannot be less than zero. Parameter name: length This is how I have it: // Set the start date myAppointment.Start = Convert.ToDateTime(StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")) + StartTimeSeed.Substring(StartTimeSeed.IndexOf("T") + 1, StartTimeSeed.Length - StartTimeSeed.Substring(0, StartTimeSeed.IndexOf("T")).Length - 1)); Fishman 01-09-2009 08:04 AM In reply to Hilary Stoupa Joined on 06-20-2008 Posts 13,297 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact The error probably is because one of the calculations that is providing the length argument of the substring is negative (i.e., less than zero). So, now the magic question is "Which one?" I see a plus sign where I think you might want a comma. Convert.ToDateTime(StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")), StartTimeSeed.Substring(StartTimeSeed.IndexOf("T")+1, StartTimeSeed.Length- StartTimeSeed.Substring(0, StartTimeSeed.IndexOf("T")).Length-1)) Try that instead. Notice after the substring that gets the date: StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")) there is now a comma instead of a plus. Hilary Stoupa 01-09-2009 08:22 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Hello Hilary! Good to hear from you! Getting a couple of different errors now: Error 1 The best overloaded method match for 'System.Convert.ToDateTime(object, System.IFormatProvider)' has some invalid arguments C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs 150 35 Customer Care - no code Error 2 Argument '2': cannot convert from 'string' to 'System.IFormatProvider' C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs 150 110 Customer Care - no code Just to be sure that I didn't goof it up, here is what I have: // Set the start datemyAppointment.Start = Convert.ToDateTime(StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")), StartTimeSeed.Substring(StartTimeSeed.IndexOf("T") + 1, StartTimeSeed.Length - StartTimeSeed.Substring(0, StartTimeSeed.IndexOf("T")).Length - 1)); Fishman 01-09-2009 08:49 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact I have made a little progress. The data that comes from the date field looks correct - IP is leaving the time off of it so there is no 'T' so I just left that part of the code out. In the time field, the data looks like this 12/30/1899 3:30:00 PM so it doesn't have a T either. I am making the assumption that the IndexOf("T") is looking for a T in the data, right? So just for goofs, I tried a space to see if that would work and I get the same 'less than zero' error, but if I remove the whole StartTimeSeed part, I get the date OK and the appointment is inserted but the time is 12 am (default I assume because we aren't passing it). Fishman 01-09-2009 09:50 AM In reply to BuildPro Guru Joined on 02-27-2008 Beavercreek, Ohio, USA Posts 77 Re: String was not recognized as a valid DateTime. Mark as Not AnswerMark as Answer... Reply Contact Hilary, I think that you may be proud of me. I figured it out. Appatently, the myAppointment.Start is looking for yyyy-mm-ddThh:mm:ss am(or pm). My time data looks like this 12/30/1899 12:00:00 AM(or PM) with the date always being the same because it is missing from the user input. So, I added the T and then stripped off everything 11 spaces after the first '1' like this: // Set the start date myAppointment.Start = Convert.ToDateTime(StartDateSeed +"T"+ StartTimeSeed.Substring(StartTimeSeed.IndexOf("1")+11)); I knew that taking your advice and checking out the stuff from MS would help! Thanks Hilary! Fishman Page 1 of 2 (16 items) 1 2 Next > Copyright © 2003-2019 Qdabra Software. All rights reserved.View our Terms of Use.
Hello Everyone. I am creating the ability to set an appointment in someone's calendar and use values in the form to set the attributes of the appoinement. I am getting stuck trying to convert two fields to DateTime. I get this error:
System.FormatException was unhandled by user code Message="String was not recognized as a valid DateTime." Source="mscorlib" StackTrace: at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider) at System.Convert.ToDateTime(String value) at Customer_Care___no_code.FormCode.CTRL263_8_Clicked(Object sender, ClickedEventArgs e) in C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs:line 150 at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent) at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)
Here is the bit of code that is generating the error.
//Get Values for Date/Time
myAppointment.Start =
Any help would be greatly appreciated.
Hi Guru,
Instead of myAppointment.Start = Convert.ToDateTime(StartDateSeed+StartTimeSeed); , use the following code line.
myAppointment = Convert.ToDateTime(StartDateSeed+ "T" +StartTimeSeed);
Good Morning Swathip. Thank you for your assistance, but now I am getting this error:
Cannot implicitly convert type 'System.DateTime' to 'Microsoft.Office.Interop.Outlook._AppointmentItem' C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs 150 29 Customer Care - no code
Additionally, I have found that if I omit just the +StartTimeSeed I get the appointment with the correct date, but a time of 12 am - I assume because the time element is missing.
Actually what is the data type of myAppointment?
myAppointment is referrming to the AppointmentItem Object for creating the appointment in the calendar. Here is the rest of the code - maybe that will help:
//Add a reference to Microsoft Outlook 12.0 Object Library. Create new instance of Microsoft.Office.Interop.Outlook.Application object:
Microsoft.Office.Interop.Outlook.
//Create an instance of AppointmentItem object and set the properties:
myAppointment.Subject = (SubjectSeed1+SubjectSeed2+SubjectSeed3);
myAppointment.Body = BodySeed1;
myAppointment.Location = (LocationSeed1 + LocationSeed2);
myAppointment.Duration = 60;
myAppointment.ReminderMinutesBeforeStart = 15;
olBusy
olFree
olOutOfOffice
olTentative
*/
myAppointment.Save();
I think I may have figured it out. the two fields that I am using to create the myAppointment.Start are both date/time fields. I have formatted them to display just the date in one field and just the time in the other field, but i think that IP is putting the date and the time in both fields, but just not displaying it. Do you know how to strip the time off the first field and the date off the second field?
Hi,
Change the first field data type to Date instead of Date and Time. Similarly the second field data type to time instead of Date and Time.
That was the first thing that I thought of, but there are complications. I am getting the data from/storing to a SQL DB as well as publishing the form to a SharePoint forms library. The two fields in question are promoted to columns in the forms library and if I don't have them formatted as DateTime, SP tries to stick the missing element in there for me so instead of the date I get 2009-01-09T00:00:00 and instead of the time I get 1899-1-1T09:05:30. It is odd I know and probably something to do with our SP configuration, but that is something that I do not have any control over, so here I am.
You can get the only date value from the date and time field using the following.
StartDateSeed.Substring(0, StartDateSeed.IndexOf("T"))
And the time from the date and time field using
StartTimeSeed.Substring(StartTimeSeed.IndexOf("T")+1, StartTimeSeed.Length- StartTimeSeed.Substring(0, StartTimeSeed.IndexOf("T")).Length-1)
Sorry to be such a pain, but I am afraid that I do not know much about writing code. I am now getting this error:
Length cannot be less than zero. Parameter name: length
This is how I have it:
// Set the start date
The error probably is because one of the calculations that is providing the length argument of the substring is negative (i.e., less than zero). So, now the magic question is "Which one?" I see a plus sign where I think you might want a comma.
Convert.ToDateTime(StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")), StartTimeSeed.Substring(StartTimeSeed.IndexOf("T")+1, StartTimeSeed.Length- StartTimeSeed.Substring(0, StartTimeSeed.IndexOf("T")).Length-1))
Try that instead. Notice after the substring that gets the date: StartDateSeed.Substring(0, StartDateSeed.IndexOf("T")) there is now a comma instead of a plus.
Hello Hilary! Good to hear from you! Getting a couple of different errors now:
Error 1 The best overloaded method match for 'System.Convert.ToDateTime(object, System.IFormatProvider)' has some invalid arguments C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs 150 35 Customer Care - no code
Error 2 Argument '2': cannot convert from 'string' to 'System.IFormatProvider' C:\Documents and Settings\dfisher\My Documents\InfoPath Projects\Customer Care Cincinnati\FormCode.cs 150 110 Customer Care - no code
Just to be sure that I didn't goof it up, here is what I have:
I have made a little progress. The data that comes from the date field looks correct - IP is leaving the time off of it so there is no 'T' so I just left that part of the code out. In the time field, the data looks like this 12/30/1899 3:30:00 PM so it doesn't have a T either. I am making the assumption that the IndexOf("T") is looking for a T in the data, right? So just for goofs, I tried a space to see if that would work and I get the same 'less than zero' error, but if I remove the whole StartTimeSeed part, I get the date OK and the appointment is inserted but the time is 12 am (default I assume because we aren't passing it).
Hilary, I think that you may be proud of me. I figured it out. Appatently, the myAppointment.Start is looking for yyyy-mm-ddThh:mm:ss am(or pm). My time data looks like this 12/30/1899 12:00:00 AM(or PM) with the date always being the same because it is missing from the user input. So, I added the T and then stripped off everything 11 spaces after the first '1' like this:
I knew that taking your advice and checking out the stuff from MS would help! Thanks Hilary!