|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Strange Date ProblemGiven that variable dt = "3/31/2007", why does it produce the following
exception on some machines? It works fine on my PC, but others have sent me this exception information because it threw up on their PC. DLDT.Columns("Date").DataType = GetType(Date) dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString System.ArgumentException: The string was not recognized as a valid DateTime. Couldn't store <3/31/2007> in Date Column. Expected type is DateTime. ---> System.FormatException: The string was not recognized as a valid DateTime. Terry Olsen wrote:
> Given that variable dt = "3/31/2007", why does it produce the following The date format was not recognised by the database. This is common when > exception on some machines? It works fine on my PC, but others have sent me > this exception information because it threw up on their PC. > > DLDT.Columns("Date").DataType = GetType(Date) > dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString > > System.ArgumentException: The string was not recognized as a valid DateTime. > Couldn't store <3/31/2007> in Date Column. Expected type is DateTime. ---> > System.FormatException: The string was not recognized as a valid DateTime. > using this type of date format without specifying a culture when converting it. Specify a Culture (or FormatInfo) when parsing the string, and don't convert it back to a string before putting it in the table.
Show quote
Hide quote
>> Given that variable dt = "3/31/2007", why does it produce the following Will this work?>> exception on some machines? It works fine on my PC, but others have sent >> me this exception information because it threw up on their PC. >> >> DLDT.Columns("Date").DataType = GetType(Date) >> dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString >> >> System.ArgumentException: The string was not recognized as a valid >> DateTime. >> Couldn't store <3/31/2007> in Date Column. Expected type is >> DateTime. ---> >> System.FormatException: The string was not recognized as a valid >> DateTime. >> > > The date format was not recognised by the database. This is common when > using this type of date format without specifying a culture when > converting it. > > Specify a Culture (or FormatInfo) when parsing the string, and don't > convert it back to a string before putting it in the table. dr.Item("Date")=Date.Parse(dt.Trim,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat) Along the same lines, I have this code:
DLDT.Columns("FileSize").DataType = GetType(Double) dr.Item("FileSize") = CType(stringVar,Double) And while checking to see if my Date change worked by changing the language settings, the FileSize column went blank. So I guess I need to figure out how to make the FileSize column global... Terry Olsen wrote:
> Along the same lines, I have this code: The decimal separator or a floating point number is also culture > > DLDT.Columns("FileSize").DataType = GetType(Double) > dr.Item("FileSize") = CType(stringVar,Double) > > And while checking to see if my Date change worked by changing the language > settings, the FileSize column went blank. So I guess I need to figure out > how to make the FileSize column global... > dependand. Use the Double.Parse method to parse the string, so that you can specify the culture to use for the conversion. Terry Olsen wrote:
Show quoteHide quote >>> Given that variable dt = "3/31/2007", why does it produce the following Using CurrentCulture will give you the same effect as not specifying any >>> exception on some machines? It works fine on my PC, but others have sent >>> me this exception information because it threw up on their PC. >>> >>> DLDT.Columns("Date").DataType = GetType(Date) >>> dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString >>> >>> System.ArgumentException: The string was not recognized as a valid >>> DateTime. >>> Couldn't store <3/31/2007> in Date Column. Expected type is >>> DateTime. ---> >>> System.FormatException: The string was not recognized as a valid >>> DateTime. >>> >> The date format was not recognised by the database. This is common when >> using this type of date format without specifying a culture when >> converting it. >> >> Specify a Culture (or FormatInfo) when parsing the string, and don't >> convert it back to a string before putting it in the table. > > Will this work? > > dr.Item("Date")=Date.Parse(dt.Trim,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat) > > culture at all, as that is what's used by default. CurrentCulture contains the culture selected by the user, and it will differ on different computers. Create a CultureInfo object for the culture that you want to use for the conversion. >> Will this work? Ok, now I'm getting wierd stuff.>> >> dr.Item("Date")=Date.Parse(dt.Trim,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat) >> > Using CurrentCulture will give you the same effect as not specifying any > culture at all, as that is what's used by default. CurrentCulture contains > the culture selected by the user, and it will differ on different > computers. Create a CultureInfo object for the culture that you want to > use for the conversion. Dim CI As System.Globalization.CultureInfo I've tried this... CI = System.Globalization.CultureInfo.CreateSpecificCulture("en-US") and this... CI = System.Globalization.CultureInfo.GetCultureInfo("en-US") and then this... dr.Item("Date") = Date.Parse(dt.Trim, CI.DateTimeFormat) So now, if I go into my Region and Language options and change the standards to something other than English (United States), sometimes it works, sometimes it doesn't. Like when I set it to Zulu, the program blocks the CPU and I have to stop the debugger. I must still be doing something wrong... > So now, if I go into my Region and Language options and change the I found that it's hanging up at the following line of code:> standards to something other than English (United States), sometimes it > works, sometimes it doesn't. Like when I set it to Zulu, the program > blocks the CPU and I have to stop the debugger. Dim wres As HttpWebResponse = wreq.GetResponse I put a messagebox before and after this line. I get the before, but not the after. So must some sort of globalization be used on this as well? Terry Olsen wrote:
>> So now, if I go into my Region and Language options and change the Did you wait to see if you get a an error of WebExceptionStatus.Timeout or >> standards to something other than English (United States), sometimes >> it works, sometimes it doesn't. Like when I set it to Zulu, the >> program blocks the CPU and I have to stop the debugger. > > I found that it's hanging up at the following line of code: > > Dim wres As HttpWebResponse = wreq.GetResponse > > I put a messagebox before and after this line. I get the before, but > not the after. something else? Does anything appear in the logs of the server you're querying? You need to put all web requests in try...catch blocks, for example with a POST request you might do something like this:- Private Function doSearch(ByVal myUrl As String, ByVal params As String) As String Dim wReq As System.Net.HttpWebRequest = CType(WebRequest.Create(myUrl), HttpWebRequest) Dim wResp As WebResponse Dim wRespStream As Stream Dim fail As Boolean = False Dim result As StringBuilder = New StringBuilder With wReq .Method = "POST" .ContentType = "application/x-www-form-urlencoded" .ContentLength = Len(params) .KeepAlive = False ' give it 60000ms to respond .Timeout = 60000 End With Dim myWriter As New StreamWriter(wReq.GetRequestStream()) Try myWriter.Write(params) Catch e As Exception fail = True With result .Append("<p>Failed to initiate search request. Please try again in a few moments.</p>") .Append("<p>If the problem persists, please contact the IT Helpdesk.</p>") End With Finally myWriter.Close() End Try Try wResp = wReq.GetResponse() Catch objEx As Exception fail = True If objEx.Equals(WebExceptionStatus.Timeout) Then result.Append("<p>Request timed out.</p>") End If With result .Append("<p>Communication failure: " & objEx.Message & "</p><p>Please close your browser and try again in a few minutes.</p>").Append("<p>If the problem persists, please contact the IT Helpdesk.</p>") End With End Try If Not (fail) Then Try wRespStream = wResp.GetResponseStream() Catch fail = True With result .Append("<p>No response to search request. Please try again in a few moments.<p>") .Append("<p>If the problem persists, please contact the IT Helpdesk.</p>") End With End Try If Not (fail) Then Dim reader As New StreamReader(wRespStream, Encoding.ASCII) result.Append(reader.ReadToEnd()) reader.Close() wRespStream.Close() End If End If Return result.ToString End Function 'doSearch (I'm sure there's something iffy with the logic somewhere in there, but it works well enough :-) Andrew > Did you wait to see if you get a an error of WebExceptionStatus.Timeout or I let it go for about 20 minutes while I ate dinner. It never threw, just > something else? blocked the CPU the entire time. I finally had to stop it. It happens only on some regional settings, not on others. Perhaps Windows is not allowing to communicate with a U.S. web site from certain regions? > Does anything appear in the logs of the server you're querying? Don't know. I'm not querying my server. I have written permission to query the server in question though. > You need to put all web requests in try...catch blocks, Yes. It's in a try/catch block. But it never catches. I'm not currently setting a Timeout value for the request so I'll try that and see what happens. Thanks, Terry Terry Olsen wrote:
Show quoteHide quote >>> Will this work? Why not simply:>>> >>> dr.Item("Date")=Date.Parse(dt.Trim,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat) >>> >> Using CurrentCulture will give you the same effect as not specifying any >> culture at all, as that is what's used by default. CurrentCulture contains >> the culture selected by the user, and it will differ on different >> computers. Create a CultureInfo object for the culture that you want to >> use for the conversion. > > Ok, now I'm getting wierd stuff. > > Dim CI As System.Globalization.CultureInfo > > I've tried this... > CI = System.Globalization.CultureInfo.CreateSpecificCulture("en-US") > and this... > CI = System.Globalization.CultureInfo.GetCultureInfo("en-US") CI = New System.Globalization.CultureInfo("en-US") > and then this... A CultureInfo object is also a format provider:> dr.Item("Date") = Date.Parse(dt.Trim, CI.DateTimeFormat) dr.Item("Date") = Date.Parse(dt.Trim, CI) Show quoteHide quote > So now, if I go into my Region and Language options and change the standards > to something other than English (United States), sometimes it works, > sometimes it doesn't. Like when I set it to Zulu, the program blocks the CPU > and I have to stop the debugger. > > I must still be doing something wrong... > > Terry,
Did you ever tried the method CDate from the extendened Net Library Microsoft Visual Basic. Almost all conversion functions have no relation anymore with VB6 but are optimized Net ones. Cor Show quoteHide quote "Terry Olsen" <tolse***@hotmail.com> schreef in bericht news:OTwAS1yiHHA.4520@TK2MSFTNGP02.phx.gbl... > Given that variable dt = "3/31/2007", why does it produce the following > exception on some machines? It works fine on my PC, but others have sent > me this exception information because it threw up on their PC. > > DLDT.Columns("Date").DataType = GetType(Date) > dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString > > System.ArgumentException: The string was not recognized as a valid > DateTime. > Couldn't store <3/31/2007> in Date Column. Expected type is > DateTime. ---> > System.FormatException: The string was not recognized as a valid DateTime. > > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: 'CDate' is a language feature, not a library feature.> Did you ever tried the method CDate from the extendened Net Library > Microsoft Visual Basic. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Herfried,
Are you sure than I cannot use it in C# in my idea? Cor Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht news:uYofk62iHHA.4936@TK2MSFTNGP03.phx.gbl... > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: >> Did you ever tried the method CDate from the extendened Net Library >> Microsoft Visual Basic. > > 'CDate' is a language feature, not a library feature. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: Yes.> Are you sure than I cannot use it in C# in my idea? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Herfried,
I meant the same as Seth, do they use in Austria the word Yes if the mean Nein? Cor Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht news:%23MESZP%23iHHA.3264@TK2MSFTNGP04.phx.gbl... > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: >> Are you sure than I cannot use it in C# in my idea? > > Yes. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Show quote
Hide quote
On May 1, 1:10 am, "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> You won't find a CDate() function in C#, but according to ILwrote: > Herfried, > > Are you sure than I cannot use it in C# in my idea? > > Cor > > "Herfried K. Wagner [MVP]" <hirf-spam-me-h...@gmx.at> schreef in berichtnews:uYofk62iHHA.4***@TK2MSFTNGP03.phx.gbl... > > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: > >> Did you ever tried the method CDate from the extendened Net Library > >> Microsoft Visual Basic. > > > 'CDate' is a language feature, not a library feature. > > > -- > > M S Herfried K. Wagner > > M V P <URL:http://dotnet.mvps.org/> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> > Are you sure than I cannot use it in C# in my idea? Disassembler when you use CDate in VB it generates the following: valuetype [mscorlib]System.DateTime [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToDate(string) So it seems you could just add a reference to Microsoft.VisualBasic in your C# project and then call the Conversions.ToDate() function to receive the same functionality of VB's CDate. Thanks, Seth Rowe Cor Ligthert [MVP] wrote:
> Terry, From what I can see, using CDate you can't specify what culture to use > > Did you ever tried the method CDate from the extendened Net Library > Microsoft Visual Basic. > Almost all conversion functions have no relation anymore with VB6 but are > optimized Net ones. > > Cor > for the conversion, and that's exactly what the OP needs.
there is no 64-bit Jet (MS Access) OLEDB driver.
Problem with: Use the following method to smooth edges of screen fonts: if ClearType is selected COM problem Persistent settings Publishing problem Can someone help me with this menu? a vb calculator, but with a twist.?? Send keystroke to a datagridview control vb.net c# mix Forms |
|||||||||||||||||||||||