cancel
Showing results for 
Search instead for 
Did you mean: 

Regional browser options and effect of server side code

Former Member
0 Kudos

I have a international portal application that I'm experiencing problems with users who don't have their options set to English(US). For example I'm displaying a date in a label as a string in (mm/dd/yyyy) format. When I execute a CDate() function in the server code when the user has a different regional setting with a different date format I'm receiving errors on the CDate function. It seems like the PDK is doing some interpreting of the data before the CDate(). I'm also experiencing problems with decimals because some regions use a decimal and others use a comma. Is there a setting or some code that I can use to change the behavior?

Accepted Solutions (1)

Accepted Solutions (1)

rima-sirich
Advisor
Advisor
0 Kudos

Hi Kevin,

I would like to reproduce the scenario you have described.

Could you, please, paste the code that fails ?

Thanks,

Rima.

Former Member
0 Kudos

I'm still having issues with the decimal formatting.

We need the user to enter a value in our (US) decimal format in a textbox. We then compare that result against a value to make sure it is within a desired tolerance. Set your browser to Spanish (Chile) or any other region where they use a , instead of a . for decimals. For this example lets just assume that the desired amount is 125.458 and our tolerance is %10

Dim dOverTol, dUnderTol, dOrigQty, dNewQty, dDiff, dPercentDif As Decimal

If CType(125.458, Decimal) <> CType(txtAmount.Text, Decimal) Then

dOverTol = CType(10, Decimal) / 100

dUnderTol = CType(10, Decimal) / 100

dOrigQty = CType(125.458, Decimal)

dNewQty = CType(txtAmount.Text, Decimal)

'obtain absolute value of difference

dDiff = Math.Abs(dOrigQty - dNewQty)

'obtain percent difference

dPercentDif = dDiff / dOrigQty

If dPercentDif > dOverTol Or dPercentDif > dUnderTol Then

lblResult.ForeColor = System.Drawing.Color.Red

lblResult.Text = "The amounts are not within 10% please try again."

Else

lblResult.Text = "The amounts are within %10"

End If

The issue that I'm running into here is that if the code is run on the portal and the browser is any non US that uses a , instead of a . the validation doesn't work because something the CType doesn't work. If I run the same code on a regular webserver with the same foreign browser it works fine.

The date formatting issue I solved on my own by treating the date like a string and parsing it to the format I need but here is an example. Set your browser to Spanish(Chile) or any other setting that treats dates as dd/mm/yyyy. Create a text box on a page and enter a date in US format "mm/dd/yyyy" and make sure the dd > 12. Submit the page to server and on the server execute the following code:

Dim theResult as string

theResult = formatSAPDate(CDate(txtDateSample.Text))

Public Function formatSAPDate(ByVal sDate As Date) As String

Dim sRetDate As String

Try

sRetDate = sDate.ToString("yyyyMMdd")

Catch ex As Exception

sRetDate = CStr(sDate)

End Try

Return sRetDate

End Function

If your executing this page on a SAP Portal you should get an error on the CDate() function. If you run this page on a regular webserver there are no problems.

Let me know if you have any other quesitons.

Kevin

Former Member
0 Kudos

I've discovered from more testing that my tolerance values are populating as decimals. The tolerances are coming from SAP and are being written to hidden text fields on the page. In most cases the value is 10.0 that's why I hardcoded it for the example.

dOverTol = CType(10, Decimal) / 100

dUnderTol = CType(10, Decimal) / 100

the actual values are:

dOverTol = CType(10.0, Decimal) / 100

dUnderTol = CType(10.0, Decimal) / 100

rima-sirich
Advisor
Advisor
0 Kudos

Hi Kevin,

I think that using of System.Globalization classes will solve the issue like in the following example:

CultureInfo ci = CultureInfo.CurrentCulture;

NumberFormatInfo nfi = ci.NumberFormat;

Double d = 10.1;

string s = d.ToString(nfi);

For Spanish(Chile) s = 10,1

For English(US) s = 10.1

Regards,

Rima.

Former Member
0 Kudos

I don't think this will solve the problem. In our application the tolerances and original values are coming from SAP and being written to hidden text fields and a label. The user is supposed to enter the quantity shipped in a text box. When the form is submitted we compare the value shipped to the original quantity and make sure it is within the tolerances that are set for that customer. All of the calculation is done on our US based webserver. The problem is occurring when I'm trying to perform calculations based on the values they enter and the hidden fields when they come from a different region. For example if my over tolerance coming from SAP is written to a text box as a string value of 10.5 when I attempt to turn that into a percent value using the function

Dim dOverTol as Decimal

dOverTol = CType(txtOverTol.Text, Decimal) / 100

If the user is in Indonessia, Chile, or another location that uses the , as decimal the code above will create a percent value of 105% when it should only be 10.5%. As I said before this doesn't happen with other applications that aren't using the PDK. Do you have any other suggestions?

Thanks for your help,

Kevin

rima-sirich
Advisor
Advisor
0 Kudos

Hi Kevin,

I suggest you to write simple Portal Application that writes to response current culture info.

Add to Page_Load function this line: Write(CultureInfo.CurrentCulture.DisplayName);

Run this PortalComponent for different culture browser settings: Spanish (Chile), German (Austria), etc. You will see that current culture changes as browser settings change. PDK runtime current culture settings depend on browser culture settings, therefore, it expects in your case to receive data from hidden field in Spanish number format and not in English data format as it receives. Therefore, formatting data using current culture number format will solve this issue.

Regards,

Rima.

Former Member
0 Kudos

Rima,

I understand what to do I'm just running into problems implementing it.

I have creaated the following function:

Public Function currCultDisplay(ByVal theValue As String) As String

Dim ci As Globalization.CultureInfo = CultureInfo.CurrentCulture

Dim nfi As NumberFormatInfo

Dim d As Double = CType(theValue, Double)

'Dim d As Double = 10.1

nfi = ci.NumberFormat

Return d.ToString(nfi)

End Function

It works fine for some conversions but for others it doesn't. The values for the function are coming from SAP to .NET as strings formatted to 3 decimal places. Some values convert correctly others don't. For example when the value coming from SAP that is passed into the function is 160.000 with the browser set to Spanish(Chile) format the function returns the incorrect value.

Do you have any suggestions on how to fix this?

rima-sirich
Advisor
Advisor
0 Kudos

Hi Kevin,

I would advice you to try this:

Dim ci As Globalization.CultureInfo = CultureInfo.CurrentCulture

Dim nfi As NumberFormatInfo = ci.NumberFormat

Dim d As Double = CType(theValue.ToString(nfi), Double)

Return d.ToString()

Regards,

Rima.

Former Member
0 Kudos

Rima,

I changed my function to the following:

Public Function currCultDisplay(ByVal theValue As String) As String

Dim ci As Globalization.CultureInfo = CultureInfo.CurrentCulture

Dim nfi As NumberFormatInfo = ci.NumberFormat

Dim d As Double = CType(theValue.ToString(nfi), Double)

Return d.ToString()

End Function

The value from SAP that I'm passing to the function is 160.000 (US). When the page is displayed with the browser set to Spanish-Chile the value the result that is written in my datagrid is 160000

Based on the the Spanish-Chile number format the value should display 160,000

Can you think of anything else I can try?

Thanks,

Kevin

rima-sirich
Advisor
Advisor
0 Kudos

Hi Kevin,

After thinking about it again ( and testing I believe that following proposition will solve this issue - we need to tell to the runtime that it gets a string in "en-US" format and not in current culture format as it expects:

Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")

Dim nfi As NumberFormatInfo = ci.NumberFormat

Dim d As Double = Convert.ToDouble(theValue, nfi)

Return d.ToString()

I tested this code with theValue = "160.004". It worked. For Spanish format, it returned 160,004. As for 160.000, it returned 160.

Regards,

Rima.

Former Member
0 Kudos

Changing the string into a US format number first worked. I then created another function that inputs a string and changes it back to the US format for input into SAP. I'm including both functions in case anyone needs them for future use.

Thanks,

Kevin

Public Function currCultDisplay(ByVal theValue As String) As String

'convert incoming string to US number

Dim ciUS As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")

Dim nfiUS As NumberFormatInfo = ciUS.NumberFormat

Dim d As Double = Convert.ToDouble(theValue, nfiUS)

'convert us number to current culture

Dim ci As CultureInfo = CultureInfo.CurrentCulture

Dim nfi As NumberFormatInfo = ci.NumberFormat

Return d.ToString(nfi)

End Function

Public Function usCultureDisplay(ByVal theValue As String) As String

'convert incoming string to current culture number

Dim ci As CultureInfo = CultureInfo.CurrentCulture

Dim nfi As NumberFormatInfo = ci.NumberFormat

Dim d As Double = Convert.ToDouble(theValue, nfi)

'convert current culture number to US format

Dim ciUS As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")

Dim nfiUS As NumberFormatInfo = ciUS.NumberFormat

Return d.ToString(nfiUS)

End Function

Answers (0)