Strange Date Problem

T

Terry Olsen

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Terry said:
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.

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.
 
C

Cor Ligthert [MVP]

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
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Did you ever tried the method CDate from the extendened Net Library
Microsoft Visual Basic.

'CDate' is a language feature, not a library feature.
 
T

Terry Olsen

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
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)
 
T

Terry Olsen

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...
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Terry said:
Will this work?

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Terry said:
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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cor said:
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.
 
R

rowe_newsgroups

Herfried,

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

Cor

'CDate' is a language feature, not a library feature.
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
 
T

Terry Olsen

Will this work?
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")

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...
 
T

Terry Olsen

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?
 
A

Andrew Morton

Terry said:
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
 
T

Terry Olsen

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Terry said:
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")

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)
 
C

Cor Ligthert [MVP]

Herfried,

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

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top