Home All Groups Group Topic Archive Search About
Author
30 Apr 2007 1:41 PM
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.

Author
30 Apr 2007 2:13 PM
Göran_Andersson
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/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.

--
Göran Andersson
_____
http://www.guffa.com
Author
1 May 2007 3:35 AM
Terry Olsen
Show quote Hide quote
>> 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.

Will this work?

dr.Item("Date")=Date.Parse(dt.Trim,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
Author
1 May 2007 3:55 AM
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...
Author
1 May 2007 1:18 PM
Göran_Andersson
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
Author
1 May 2007 1:15 PM
Göran_Andersson
Terry Olsen wrote:
Show quoteHide quote
>>> 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.
>
> 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.

--
Göran Andersson
_____
http://www.guffa.com
Author
2 May 2007 2:40 AM
Terry Olsen
>> 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.

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...
Author
2 May 2007 2:59 AM
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?
Author
2 May 2007 9:53 AM
Andrew Morton
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
Author
2 May 2007 12:51 PM
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
Author
2 May 2007 6:24 PM
Göran_Andersson
Terry Olsen wrote:
Show quoteHide quote
>>> 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.
>
> 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)

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


--
Göran Andersson
_____
http://www.guffa.com
Author
30 Apr 2007 3:17 PM
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

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.
>
>
>
Author
30 Apr 2007 9:29 PM
Herfried K. Wagner [MVP]
"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/>
Author
1 May 2007 5:10 AM
Cor Ligthert [MVP]
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/>
Author
1 May 2007 11:28 AM
Herfried K. Wagner [MVP]
"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/>
Author
4 May 2007 1:25 AM
Cor Ligthert [MVP]
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/>
Author
1 May 2007 2:38 PM
rowe_newsgroups
Show quote Hide quote
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.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?

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
Author
1 May 2007 1:27 PM
Göran_Andersson
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