date rendered differently on two computers with same regional settings

M

Mark

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.

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?

2) what can i do to solve this problem with server 2003?

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

=> with XP: 13/07/2007
=> with server 2003: 7/13/2007 !!!

Thanks for help
Marc
 
?

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

Mark said:
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.
 
M

Mark

Hi, thanks for replying

It's an asp.net error ("Server error in application ....)
And, as you can see in the code, i use parameter in the Update command.

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?




Göran Andersson said:
Mark said:
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
 
?

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

Mark said:
Hi, thanks for replying

It's an asp.net error ("Server error in application ....)

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?

e.NewValues("datbeg") = datbeg
 
M

Mark

This is the complete message i get:

Server Error in '/myappl' Application.
--------------------------------------------------------------------------------

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.

Source Error:


Line 17: datbeg = e.NewValues("datbeg")
Line 18: datend = e.NewValues("datend")
Line 19: lec2 = e.NewValues("lector2")
Line 20: e.NewValues("datbeg") = String.Format("{0:yyyy.MM.dd}",
datbeg)

Source File: C:\Inetpub\wwwroot\myapp\test.aspx.vb Line: 18

Stack Trace:

[InvalidCastException: Conversion from string "13-08-07" to type 'Date' is
not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)
+222
Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(Object Value)
+175
wijzig.DetailsView1_ItemUpdating(Object sender,
DetailsViewUpdateEventArgs e) in
C:\Inetpub\wwwroot\enquetesql\wijzig.aspx.vb:18
System.Web.UI.WebControls.DetailsView.OnItemUpdating(DetailsViewUpdateEventArgs
e) +133
System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg,
Boolean causesValidation) +716
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +461
System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source,
EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source,
EventArgs e) +109
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String
eventArgument) +163
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
"Göran Andersson" <[email protected]> schreef in bericht
news:[email protected]...
 
G

Guest

This is the complete message i get:

Server Error in '/myappl' Application.
---------------------------------------------------------------------------­-----

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.

Source Error:

Line 17: datbeg = e.NewValues("datbeg")
Line 18: datend = e.NewValues("datend")
Line 19: lec2 = e.NewValues("lector2")
Line 20: e.NewValues("datbeg") = String.Format("{0:yyyy.MM.dd}",
datbeg)

Source File: C:\Inetpub\wwwroot\myapp\test.aspx.vb Line: 18

Stack Trace:

[InvalidCastException: Conversion from string "13-08-07" to type 'Date' is
not valid.]


Just a quick though on this,

try to use ParseExact

yourdateobj = DateTime.ParseExact("13-08-07", "dd-MM-yy",
System.Globalization.CultureInfo.InvariantCulture)

or use an appropriate culture
 
M

Mark

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

Guest

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
 
C

Chris

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

Guest

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?

Because 31/09/2007 doesn't exist. The month of September has 30
days :)
 
G

Guest

This:

<asp:textbox id="TextBox1" runat=server columns="45" Width="125px"></
asp:textbox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="your date invalid"
Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
<asp:Button id="Button1" text="Submit" runat="server" />

gives me the message from validator: "your date invalid" even I typed
a wrong text.

So, I guess that "string was not recognized as ...." returned in your
case in the code-behind class and not by the validator control
 
M

Mark

Ok, thanks.

Anon User said:
This:

<asp:textbox id="TextBox1" runat=server columns="45" Width="125px"></
asp:textbox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="your date invalid"
Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
<asp:Button id="Button1" text="Submit" runat="server" />

gives me the message from validator: "your date invalid" even I typed
a wrong text.

So, I guess that "string was not recognized as ...." returned in your
case in the code-behind class and not by the validator control
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top