Re: date rendered differently on two computers with same regional settings



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>
<asp:Parameter Name="datbeg" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>

<asp:DetailsView ....>
<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
.


Loading