date rendered differently on two computers with same regional settings

the same application runs independantely on two computers:
the first is an Windows XP prof. sp2 IIS 5.1, dutch version with regional
settings = French (Belgium). So the short date notation is e.g. 13/08/2007.
The date (datetime) in sql server installed on this computer is also
rendered as 13/08/2007.

the second computer is a Windows server 2003 sp2 IIS 6.0, english version
with also regional settings = French (Belgium). So the short date notation
is e.g. 13/08/2007. The date (datetime) in sql server installed on this
computer is also rendered as 13/08/2007.

Sofar no difference.

The application uses a detailsview for showing data and allows updating
data.
The date in the detailsview from the sql server is rendered correctly with
both computers (e.g. 13-08-07 because of the format, see code below).
But now, when clicking on Updating button and the date is e.g. 14-08-07, the
XP computer makes the update without problem, while the server 2003 box
gives the error:"could not convert string to datatime: out-of-range value".

I tried in code-behind with the String.Format("{0:yyyy.MM.dd}", but without
succes

So my questions are:
1) Why does the conversion from a string (data shown in textbox of the
detailsview) to datetime (in sql server) happen properly with XP box and
not with server 2003? The only difference is the language version (dutch for
xp, english for server 2003), but with same regional settings. Are there
different conversion version in function of ... what?

Advertisements

Re: date rendered differently on two computers with same regionalsettings

Mark wrote:
> Hi,
>
> the same application runs independantely on two computers:
> the first is an Windows XP prof. sp2 IIS 5.1, dutch version with regional
> settings = French (Belgium). So the short date notation is e.g. 13/08/2007.
> The date (datetime) in sql server installed on this computer is also
> rendered as 13/08/2007.
>
> the second computer is a Windows server 2003 sp2 IIS 6.0, english version
> with also regional settings = French (Belgium). So the short date notation
> is e.g. 13/08/2007. The date (datetime) in sql server installed on this
> computer is also rendered as 13/08/2007.
>
> Sofar no difference.

Yes, there are differences. It's probably the difference in language
that is the vital difference, but that is not important.

What is important is that you are relying on the regional settings at
all in the web application. The user account that is used to run the web
application is not the same user acount that you have set the regional
settings for, so you don't have control over the regional settings used.
> The application uses a detailsview for showing data and allows updating
> data.
> The date in the detailsview from the sql server is rendered correctly with
> both computers (e.g. 13-08-07 because of the format, see code below).
> But now, when clicking on Updating button and the date is e.g. 14-08-07, the
> XP computer makes the update without problem, while the server 2003 box
> gives the error:"could not convert string to datatime: out-of-range value".

Does that error come from the database or the ASP.NET code?
> I tried in code-behind with the String.Format("{0:yyyy.MM.dd}", but without
> succes

That's on the right way. You should however not convert the date to a
string when you send it to the database. You should use a parameterised
query so that you can send the date as a DateTime value.
>
> So my questions are:
> 1) Why does the conversion from a string (data shown in textbox of the
> detailsview) to datetime (in sql server) happen properly with XP box and
> not with server 2003? The only difference is the language version (dutch for
> xp, english for server 2003), but with same regional settings. Are there
> different conversion version in function of ... what?

It's just not using the settings that you think that it is. That's why
you shouldn't rely on the system settings.
> 2) what can i do to solve this problem with server 2003?

Here you are using an automatic conversion from DateTime to String. You
should do the conversions explicitly, so that you have control over when
they occur and what culture they use.
> => with XP: 13/07/2007
> => with server 2003: 7/13/2007 !!!
>
> Thanks for help
> Marc
>
>

"Göran Andersson" <> schreef in bericht
news:...
> Mark wrote:
>> Hi,
>>
>> the same application runs independantely on two computers:
>> the first is an Windows XP prof. sp2 IIS 5.1, dutch version with regional
>> settings = French (Belgium). So the short date notation is e.g.
>> 13/08/2007. The date (datetime) in sql server installed on this computer
>> is also rendered as 13/08/2007.
>>
>> the second computer is a Windows server 2003 sp2 IIS 6.0, english version
>> with also regional settings = French (Belgium). So the short date
>> notation is e.g. 13/08/2007. The date (datetime) in sql server installed
>> on this computer is also rendered as 13/08/2007.
>>
>> Sofar no difference.
>
> Yes, there are differences. It's probably the difference in language that
> is the vital difference, but that is not important.
>
> What is important is that you are relying on the regional settings at all
> in the web application. The user account that is used to run the web
> application is not the same user acount that you have set the regional
> settings for, so you don't have control over the regional settings used.
>
>> The application uses a detailsview for showing data and allows updating
>> data.
>> The date in the detailsview from the sql server is rendered correctly
>> with both computers (e.g. 13-08-07 because of the format, see code
>> below).
>> But now, when clicking on Updating button and the date is e.g. 14-08-07,
>> the XP computer makes the update without problem, while the server 2003
>> box gives the error:"could not convert string to datatime: out-of-range
>> value".
>
> Does that error come from the database or the ASP.NET code?
>
>> I tried in code-behind with the String.Format("{0:yyyy.MM.dd}", but
>> without succes
>
> That's on the right way. You should however not convert the date to a
> string when you send it to the database. You should use a parameterised
> query so that you can send the date as a DateTime value.
>
>>
>> So my questions are:
>> 1) Why does the conversion from a string (data shown in textbox of the
>> detailsview) to datetime (in sql server) happen properly with XP box and
>> not with server 2003? The only difference is the language version (dutch
>> for xp, english for server 2003), but with same regional settings. Are
>> there different conversion version in function of ... what?
>
> It's just not using the settings that you think that it is. That's why you
> shouldn't rely on the system settings.
>
>> 2) what can i do to solve this problem with server 2003?
>
> You should specify the culture that you want to use in the web.config, or
> for each conversion. Alternatively, you can use specific format strings,
> like "dd-MM-yy".
>
>> The code-behind:
>>
>> Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e
>> As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles
>> DetailsView1.ItemUpdating
>> ....
>> e.NewValues("datbeg") = String.Format("{0:yyyy.MM.dd}", datbeg)
>> ....
>>
>> the aspx file:
>> <asp:SqlDataSource ....
>> UpdateCommand="UPDATE [mytable] SET [datbeg] = @datbeg">
>> <UpdateParameters>
>> <asparameter Name="datbeg" Type="DateTime" />
>> </UpdateParameters>
>> </asp:SqlDataSource>
>>
>> <aspetailsView ....>
>> <ItemTemplate>
>> <asp:Label ID="Label1" runat="server" Text='<%# Bind("datbeg",
>> "{0:dd-MM-yy}") %>'></asp:Label>
>> </ItemTemplate>
>> </asp:TemplateField>
>>
>> <asp:TemplateField HeaderText="begin" SortExpression="datbeg">
>> <EditItemTemplate>
>> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("datbeg",
>> "{0:dd-MM-yy}") %>' ></asp:TextBox>
>> </EditItemTemplate>
>> ...
>>
>> I have another example of difference:
>>
>> sql = "select datbeg from mytable"
>> ....
>> dtreader.Read()
>> datb = dtreader.GetDateTime(0).Date
>
> Here you are using an automatic conversion from DateTime to String. You
> should do the conversions explicitly, so that you have control over when
> they occur and what culture they use.
>
>> => with XP: 13/07/2007
>> => with server 2003: 7/13/2007 !!!
>>
>> Thanks for help
>> Marc
>
>
> --
> Göran Andersson
> _____
> http://www.guffa.com

Yes, but where does the error message originate? What is the type of the
exception?
> And, as you can see in the code, i use parameter in the Update command.

Then make sure that you set the value as a DateTime value, not as a string.
> In the code-behind, i did:
> Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As
> System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles
> DetailsView1.ItemUpdating
>
> Dim datbeg As Date
> datbeg = e.NewValues("datbeg")
> e.NewValues("datbeg") = String.Format("{0:yyyy.MM.dd}", datbeg)
>
> you have an idea what to change here?

Conversion from string "13-08-07" to type 'Date' is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Conversion from string
"13-08-07" to type 'Date' is not valid.

The compare validator doesn't recognize the date "13-08-07" as valid.
When i remove it, it works. Is this a bug?
But now, when a user types a real wrong date (like 31-09-07 or just text),
he gets "the string was not a recognize as a valid datetime". Any way to
prevent that error?

Because I think the format "dd-MM-yy" is not a valid one for Belgium.
You said, the short date notation on your box is "13/08/2007", so the
control expects to get the date in "dd/MM/yy". If you want to have a
dash as a separator then you have to use the
RegularExpressionValidator Control (expression would be similar to

^\d{1,2}\-\d{1,2}\/\d{2}$

will work for

1-8-07
13-08-07

> But now, when a user types a real wrong date (like 31-09-07 or just text),
> he gets "the string was not a recognize as a valid datetime". Any way to
> prevent that error?
>

Thanks.
Ideal would be to use the compare validator with operator=DataTypeCheck".

The short notation on the computer is d/MM/yyyy.
So I changed the format into: Bind("datbeg", {0:d/MM/yyyy}")
but the compare validator still ignores it when typing e.g. 31/09/2007
Instead of getting the message of the compare validator ("your date in
invalid"), i get the whole error "string was not recognized as ...."

Do you think it's possible to use compare validator?

"Alexey Smirnov" <> schreef in bericht
news:...
> On Aug 9, 11:41 am, "Mark" <> wrote:
>> Yes, thanks, it works ... if i remove the existing compare validator like
>> this:
>>
>> <asp:CompareValidator ID="CompareValidator1" runat="server"
>> ControlToValidate="textbox7" text="this is not a valid
>> date"
>> Type=Date Operator=DataTypeCheck >
>> </asp:CompareValidator>
>>
>> The compare validator doesn't recognize the date "13-08-07" as valid.
>> When i remove it, it works. Is this a bug?
>
> Because I think the format "dd-MM-yy" is not a valid one for Belgium.
> You said, the short date notation on your box is "13/08/2007", so the
> control expects to get the date in "dd/MM/yy". If you want to have a
> dash as a separator then you have to use the
> RegularExpressionValidator Control (expression would be similar to
>
> ^\d{1,2}\-\d{1,2}\/\d{2}$
>
> will work for
>
> 1-8-07
> 13-08-07
>
>
>> But now, when a user types a real wrong date (like 31-09-07 or just
>> text),
>> he gets "the string was not a recognize as a valid datetime". Any way to
>> prevent that error?
>>
>
> You could wrap the call in a Try..Catch to catch a format error
>
> Try
> yourdateobj = DateTime.ParseExact("13-08-07", "dd-MM-yy",
> System.Globalization.CultureInfo.InvariantCulture)
> Catch ex As Exception
> yourdateobj = Nothing
> End Try
>
> Also you can use TryParse() or TryParseExact()
>
> DateTime.TryParseExact("13-08-07", "dd-MM-yy",
> CultureInfo.InvariantCulture, DateTimeStyles.None, yourdateobj)
>
> And you can use Regex
>
> Dim yourdateobj As DateTime
> Dim regDate As New System.Text.RegularExpressions.Regex("^\d{1,2}\-
> \d{1,2}\/\d{2}$")
> If regDate.IsMatch("13-08-07") Then
> yourdateobj = Date.Parse("13-08-07")
> End If
>

On Aug 9, 3:30 pm, "Chris" <> wrote:
> Thanks.
> Ideal would be to use the compare validator with operator=DataTypeCheck".
>
> The short notation on the computer is d/MM/yyyy.
> So I changed the format into: Bind("datbeg", {0:d/MM/yyyy}")
> but the compare validator still ignores it when typing e.g. 31/09/2007
> Instead of getting the message of the compare validator ("your date in
> invalid"), i get the whole error "string was not recognized as ...."
>
> Do you think it's possible to use compare validator?
>

On Aug 9, 3:59 pm, Alexey Smirnov <> wrote:
> > but the compare validator still ignores it when typing e.g. 31/09/2007
> > Instead of getting the message of the compare validator ("your date in
> > invalid"), i get the whole error "string was not recognized as ...."
>

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!