473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strange Date Problem

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/2007in Date Column. Expected type is DateTime. --->
System.FormatException: The string was not recognized as a valid DateTime.

Apr 30 '07 #1
17 2251
Terry Olsen wrote:
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/2007in 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.

--
Göran Andersson
_____
http://www.guffa.com
Apr 30 '07 #2
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

"Terry Olsen" <to******@hotmail.comschreef in bericht
news:OT**************@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/2007in Date Column. Expected type is
DateTime. --->
System.FormatException: The string was not recognized as a valid DateTime.

Apr 30 '07 #3
"Cor Ligthert [MVP]" <no************@planet.nlschrieb:
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/>
Apr 30 '07 #4
>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/2007in 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.Globaliz ation.CultureInfo.CurrentCulture.DateTimeFormat)

May 1 '07 #5
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...
May 1 '07 #6
Herfried,

Are you sure than I cannot use it in C# in my idea?

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:uY**************@TK2MSFTNGP03.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlschrieb:
>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/>

May 1 '07 #7
"Cor Ligthert [MVP]" <no************@planet.nlschrieb:
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/>
May 1 '07 #8
Terry Olsen wrote:
>>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/2007in 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.Globaliz ation.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.

--
Göran Andersson
_____
http://www.guffa.com
May 1 '07 #9
Terry Olsen wrote:
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...
The decimal separator or a floating point number is also culture
dependand. Use the Double.Parse method to parse the string, so that you
can specify the culture to use for the conversion.

--
Göran Andersson
_____
http://www.guffa.com
May 1 '07 #10
Cor Ligthert [MVP] wrote:
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
From what I can see, using CDate you can't specify what culture to use
for the conversion, and that's exactly what the OP needs.

--
Göran Andersson
_____
http://www.guffa.com
May 1 '07 #11
On May 1, 1:10 am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
Herfried,

Are you sure than I cannot use it in C# in my idea?

Cor

"Herfried K. Wagner [MVP]" <hirf-spam-me-h...@gmx.atschreef in berichtnews:uY**************@TK2MSFTNGP03.phx.gbl. ..
"Cor Ligthert [MVP]" <notmyfirstn...@planet.nlschrieb:
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?
You won't find a CDate() function in C#, but according to IL
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

May 1 '07 #12
>Will this work?
>>
dr.Item("Date")=Date.Parse(dt.Trim,System.Globali zation.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.CreateSpecificCul ture("en-US")
and this...
CI = System.Globalization.CultureInfo.GetCultureInfo("e n-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...
May 2 '07 #13
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 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.

So must some sort of globalization be used on this as well?
May 2 '07 #14
Terry Olsen wrote:
>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 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.
Did you wait to see if you get a an error of WebExceptionStatus.Timeout or
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
May 2 '07 #15
Did you wait to see if you get a an error of WebExceptionStatus.Timeout or
something else?
I let it go for about 20 minutes while I ate dinner. It never threw, just
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
May 2 '07 #16
Terry Olsen wrote:
>>Will this work?

dr.Item("Date")=Date.Parse(dt.Trim,System.Global ization.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.CreateSpecificCul ture("en-US")
and this...
CI = System.Globalization.CultureInfo.GetCultureInfo("e n-US")
Why not simply:

CI = New System.Globalization.CultureInfo("en-US")
and then this...
dr.Item("Date") = Date.Parse(dt.Trim, CI.DateTimeFormat)
A CultureInfo object is also a format provider:

dr.Item("Date") = Date.Parse(dt.Trim, CI)
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...


--
Göran Andersson
_____
http://www.guffa.com
May 2 '07 #17
Herfried,

I meant the same as Seth, do they use in Austria the word Yes if the mean
Nein?

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:%2******************@TK2MSFTNGP04.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlschrieb:
>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/>

May 4 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Irvine | last post by:
I have a small database made up of 2 tables. I have a report that is driven from a query. On the report are a couple of fields linked to memo boxes in the table. Everytime I run the report I get...
3
by: Jim in Arizona | last post by:
I'm going insane! I don't know if it's just that the .net 2.0 framework is buggy or if it really is my code. This is pretty hard to explain since I can't even begin to nail down why this is...
10
by: Trapulo | last post by:
Why Now.Date.Subtract(New Date(2000, 1, 1)).Days returns 731529?? It is a too big value a think! I aspect something as 1030-1100....
1
by: Maileen | last post by:
Hi, I finished my application butonce again i have some strange behavior with XML/text functions... for example, here below is a function which worked perfectly till now and now generate an...
2
by: Paul Furman | last post by:
I don't know, maybe this isn't strange but someone else set up the shopping card coding I'm working with, the way it works is to get the time() in seconds like 1172693735 and that's the shopper_ID...
1
by: Victor | last post by:
Hi guys, I have a very strange problem with scriptmanager here. I want to load a js (which is embed in the project) but everytime i try to load that, it gives me error like Specified argument was...
3
by: Reg143 | last post by:
Hi all, The code below loops from a starting date, incrementing the date and displaying date and day-of-week. Everything is fine until 2007-11-04 is reached. Any help would be appreciated. ...
1
by: Dan2kx | last post by:
Hello (again) i have a rather strange problem to tackle i am still doing the holiday database (for those who have helped me before) and now need (which means i have to rethink everything) to...
3
by: Tomasz J | last post by:
Hello Developers, I have a control derived from System.Web.UI.WebControls.WebControl. Control has this property: public string Value { set { _value = value; } get { return _value; }
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.