Regions / Locales - String Not Recognized As Valid DateTime
Feb 5, 2012
I've been having several problems lately with clients that are using a different Windows Region setting than I am. The error is as follows:
The type initializer for 'InventoryDataTable' threw an exception. ---> System.TypeInitializationException: The type initializer for 'InventoryDataTable' threw an exception. ---> System.FormatException: String was not recognized as a valid DateTime.
The error occurs when users load the application and their region settings do not match my own. The application loads up a dataset and attempts to receive a small amount of data before allowing the user to log in. When this is removed, the problem occurs immediately after the user logs in to the app. I cannot seem to find the proper settings to force the user to use either my Region, or to allow the application to figure it out on it's own...
I am trying to parse a date to a datetime picker but this error keeps on appearing "String was not recognized as a valid DateTime" The reason behind this is because the date format of the computer is "MM/dd/yyyy" whereas the date am passing is in this format "dd/MM/yyyy" Here is how I tried resolving the problem:
working with dates is driving me mad I have the following function to parse al datetime strings in my project:
Public Shared Function ToDate(ByVal obj As Object) As DateTime? If obj = String.Empty OrElse obj Is DBNull.Value Then Return Nothing Return DateTime.Parse(obj.ToString) End Function
Most of my date strings on my pc are "yyyy/MM/dd" and it parses fine. However on the server the dates are "dd/MM/yyyy".I tried this but it made no difference:
Public Shared Function ToDate(ByVal obj As Object) As DateTime? If obj = String.Empty OrElse obj Is DBNull.Value Then Return Nothing Return DateTime.Parse(obj.ToString, System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat) End Function
on my pc if I assign DateTime.Now to a textbox I get 2011/05/31 08:02:00 AM for example. On the server I have my project the date displays as 31/05/2011 08:02:00 AM. Now I use objectdatasources to save the values to my database. Yesterday I was getting this error:Cannot convert value of parameter '' from 'System.String' to 'System.DateTime'until I formatted the date value like so:
I am trying to write a bit of logic that compares a data against todays date in the row datarowbound event of a gridview. I think I am converting the row data item to datatype datetime but visual studio is telling me that I am not!
Protected Sub GvAdDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvAdDetails.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then If DataBinder.Eval(e.Row.DataItem, "dateLimit").ToString <> "NULL" Then Dim dateLimit As Date [Code] .....
We have data feed, a file with index values and dates (like the S&P 500, for instance). The dates are represented as YYYYMMDD. The Date.Parse() method doesn't understand this format, of course.I wrote a simple static method on a class to parse this, not a big deal, but it irritates the engineer in me that I can't figure out how to get the IFormatProvider functionality to work. All the examples deal with the FORMATTING step, not the PARSING step.So basically, I want to be able to say:
Dim d as DateTime = Date.Parse("20110522", New CustomFormatter())
and get the correct date value out of the Parse method.
Having implemented the IFormatProvider method on CustomFormatter, the GetFormat() method gets called with a System.Type of DateTimeFormatInfo, but I honestly have no idea what to do from there since all the example deal with formatting, not parsing. It doesn't matter if I return an object (self) or null I simply get "String was not recognized as a valid DateTime."
I'm creating an XML file from values contained in a datatable. When I try to write an XML date value, I receive "String not recognized as a valid datetime. The dates that are stored in the datatable column is in this format "yyyymmdd". I tried to use XmlConvert.ToDateTime but I get a warning about the method being obsolete and I'm not sure what serialization mode I should use. How would I format the date from the datatable to MM/dd/yyyy and write to an XML file?
I am having a major issue understanding why this code runs fine once and then when run a second time causes the "String was not recognized as a valid DateTime." exception. This program loops through folders and subdirectorys and retrieves the file dates from the last modified date of the file , Its only when i click New Report to reset all the values in the settings which clears all object lists and resets all the other values that this issue occurs.
This is the value being passed to this function: #6/14/2011 4:46:51 PM# (The file inside the subdirectory,s last modified date.)
The function is Public Sub setFileDate(ByVal fileDate As DateTime) 'Format the datetime from the filedate to a UK culture. Dim myDateTime As DateTime = DateTime.Parse(fileDate, ukCulture.DateTimeFormat) 'Set the current files date. dFileDate = myDateTime.Date [Code] .....
I recently moved a webservice VB.net project from VS 2005 to VS 2008 and now a conversion I use to create a unique receipt number is no longer working.
The Format() function which is supposedly a Visual basic function gives the error: 'Format' is a type and cannot be used as an expression.
The Year() Function which is also a VB function gives the error: Name 'Year' is not declared.
The Datetime() Function which is also a VB function gives the error: 'DateTime' is a type and cannot be used as an expression.
The actual code statement remains unchanged between the two web services but VS2005 compiles and VS2008 does not. Dim julda As Long = CLng(Format(Year(oneRowDon.Item(8)), "0000") _ + Format(DateTime("d", CDate("01/01/" _ + Format(Year(oneRowDon.Item(8)), "0000")), oneRowDon.Item(8)) _ + 1, "000"))
When you set a region equal to another, or set it equal to a new instance, do you have to call dispose on the region, or does VB do that? (I guess this isn't necessarily specific to regions)
ex:
'at some point in the code a region is created Dim Rgn as New Region(SomeRect) '....
[Code].....
If Rgn now points to the Rgns(0) clone, what happens to the original memory created by "New Region(SomeRect)"? Do I need to call dispose on Rgn before assigning it to the the Rgns(0) clone?
I know i can collapse all regions with Ctrl+M, Ctrl+O - but that also collapses all of the subs/functions/properties etc within the #Region.if it collapsed all of the sub regions too - just not the subs etc.Is there a way to collapse all top level regions only? Maybe an extension?
I always get worried and my layout when i code. I always get myself in a flap when coding and worried i should be doing it different.What i am going on about is not the code it self. But where i actually put the code.
What i am thinking about is functionality (does it matter if formload code is at top/bottom/ or even middle)
And read ability. Do i put all button events into regions?
am making a project were the user can put a specific time in a textbox. the program needs to check the system time and the time in the textboxif they are the same there needs to start an action.
I have the following string: 20111209 How do I turn this into a DateTime Value? I've tried Convert.ToDateTime(s) normally but it keeps yelling that I'm doing things wrong because the string doesn't look like a datetime value.
So, I'm trying to do this...DatumRodjenja_db = DatumRodjenja.Substring(6, 4) & "-" & DatumRodjenja.Substring(3, 2) & "-" & DatumRodjenja.Substring(0, 2)Where DatumRodjenja_db is DateTime variable, and DatumRodjenja is String. And even after that, I'm still getting DatumRodjenja_db in this format "DD.MM.YYYY."
When i click the gridview column, that selected column items should appear in the datetime picker
GridView Column value is string, datetime picker datatype is datetime
Code
Private Sub gridview1_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridview1.CellContentDoubleClick Dim i As Integer
[Code]....
When i clicked the gridview column, it is showing error as "Conversion from string "25/09/2011" to type 'Date' is not valid"
I have the following sql select statement in my code:
Code:
sql = "SELECT * " sql &= "FROM Trn_DC_All " sql &= " WHERE (Content LIKE '% PAGE%') AND Convert(datetime,Substring(Right(Trn_DC_All.Content,21),2,10),7) >= " &
[code]....
Basically, for the substring in the select satement,the datatype is string. By the way, the strFromDate and strToDate datatype are string also.Actually I would like to select the data from database whereby the Substring(Right(Trn_DC_All.Content,21),2,10) is larger than strFromDate but smaller than strToDate.The dataformat in the database is "Feb 01,09" and the strFromDate,strToDate format is "20/07/2009".What should I change in my select statement so that it can retrieve the data?
In my IDE #Region statement are left justified by the auto outlining tool. I think this is ugly. Is there anyway to make #Regions have standard indentions? I am using Visual studio 2008 version 9.0. See code block below for an example.
Need to convert a string to normal datetime format so it can be correctly inserted onto a datetime type field. String Format: 4/21/2010 4:43:03 PM Example: 2010-04-21 16:43:03.000 I have clarified question: The need is to change format and not type.