classic asp SetLocale number formats - asp-classic

I have a site that had SetLocale(1033) [english united states] however I now had to change it to Chinese SetLocale(2052). That works great in that monthname etc returns Chinese, however my number format has gone to town. Instead of 0.33 the results are .33 for example and code crashes. I think that the date format has also changed.
This is an issue as there is a LOT of code that will need to be edited accordingly. Is there a way to setLocale to Chinese but keep other format as per United States?
Much appreciated.

Use the Session.LCID to capture your formatted information then set it back to normal at the end of processing.
oldsession = Session.LCID
Session.LCID = 2057
day_uk = WeekdayName(3)
Session.LCID = 1036
day_fr = WeekdayName(3)
Session.LCID = 1034
day_es = WeekdayName(3)
Session.LCID = oldsession
response.write day_uk & " | " & day_fr & " | " & day_es
I used UK English, French and Spanish locales as the Chinese was not loaded on my server.

Related

Trouble using a For Next variable in "xxxx.Rows.Count"

In the code block below I am wishing to use a variable in a xxxx.Rows.Count line.
Specifically, I'm wanting to take the "R1200s" portion and increment it to R1201s, R1202s, R1203s and so on as the For-Next loop executes. I, of course, already have the DataTable()'s portion defined... no problem there. The code works fine if I write out every single instance but I'd like to shorten up into a simpler block of code. I've been able to use "i" in every other control with success but getting hung up on only this portion.
I've tried something like ("R120" & i & "s"), along with many variations of this but, can find no solution that works.
Any help, or a point in the right direction, is appreciated.
For i = 1 To 8
Dim Ai As Label = DirectCast(Page.FindControl("A" & ID.ToString()), Label)
Dim Si As Hyperlink = DirectCast(Page.FindControl("S" & ID.ToString()), Hyperlink)
If R1200s.Rows.Count < Slots.Rows(0)(Ri).ToString() +1 Then
Ai.Style.Add("background-color","#000000")
Si.ImageUrl = "images/" & ID & ".png"
Si.NavigateUrl = "step3.aspx?Time=" & ID & "&Date=" & Request.QueryString("Date") & "&Day=" & Request.QueryString("Day") & "&Entry=" & Request.QueryString("Entry")
End If
Next

Convert date string value from U.S. to native Date with current locale <> U.S.?

As in Convert month name into number, I want to use DateValue to create a date from a date string. However, the string I have is guaranteed to be in U.S. format, while the PC executing the code is running on German locales.
Example: DateValue fails for "10 Oct 2013 06:50:19", it would succeed for "10 Okt 2013 06:50:19" since "October" is "Oktober" in German.
I.e. I need to get the date from a string in a country-specific date format that is different from the currert locale.
How can I do that without hardcoding an English-to-German month names translation table?
Also, the code should continue to work if the workstation executing the code is reconfigured for a different locale, so I am looking for a way to switch settings temporarily to get the U.S. date via DateValue, then switch back to the original setting. Of course, that code should among Windows flavours be portable...
The code currently having this problem is shown below. It tries to get a specific server´s system time by peeking into the http header. It fails if the current (short) month name in English is different in German.
Public Function GetServerTimeFromUrl (ByVal Url,ByRef DT)
Dim HttpRequest: Set HttpRequest = CreateObject("Microsoft.XMLHttp")
HttpRequest.Open "GET" , Url, false
HttpRequest.Send
Dim Result: Result=(HttpRequest.Status = 200)
If Result then
Dim DateS: DateS=HttpRequest.getResponseHeader ("Date")
DateS=Right (DateS,Len (DateS)-InStr (DateS,",")-1)
' DateS has weekday removed
DateS=Left (DateS,InStrRev (DateS," ")-1)
' DateS has timezone removed
' DateS now holds something like "09 Sep 2013 11:49:54"
Dim ServerTimeGMT: ServerTimeGMT=DateValue (DateS)+TimeValue (DateS)
Dim Delta: Delta=CreateObject("WScript.Shell").RegRead("HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
' Delta now holds "skew" value for time zone including DST
DT=DateAdd("n", -Delta,ServerTimeGMT)
Result=true
End If
GetServerTimeFromUrl=Result
End Function
You can use GetLocale and SetLocale to change the locale:
This should work on a German system with a US date
option explicit
dim currentLocale
currentLocale = GetLocale()
SetLocale 1033 ' 1033 is the EN-US locale
msgbox datevalue("9 Oct 2013 06:50:19")
' Output: 10/9/2013
' Put back the original locale
SetLocale currentLocale
Or the other way around; a German date on a US system
SetLocale 1031 ' 1031 is the German locale
msgbox datevalue("9 Okt 2013 06:50:19")
' Output: 09.10.2013

How to handle ampersands in URL parameters?

I am having the following issue:
I am using an application that allows users to concatenate text to build a URL that passes parameters to an ASP page via GET method, i.e. something like:
http://myhostname/process.asp?param1=value1&param2=value2
Problem is value1 and value2 can contain the ampersand symbol, which is not interpreted as a text character.
The most popular solution to this issue is to encode the URL, which is not an option for me because I cannot modify the program that builds the URL. I can modify the process.asp page, but not the program that concatenates the text fields and builds the URL.
Things I've tried to search for are:
How to encode a URL using javascript directly in the browser
How to change IIS default behaviour when reading an &
Alternative ways to pass parameters, i.e. something like passing them as a single string of characters separated with pipes
Hope you can give me some guidance.
You can read the entire query string and parse it yourself, like this:
q = Request.QueryString
a = Split(q, "=")
i = 1
For Each s In a
If i mod 2 = 0 Then
If InStr(s, "&") <> InStrRev(s, "&") Then
Response.Write "Value: " & Left(s, InStrRev(s, "&") - 1) & "<br/>"
hidingParam = Right(s, Len(s) - InStrRev(s, "&"))
Response.Write "PAramName: " & hidingParam & "<br/>"
i = i + 1
Else
Response.Write "Value: " & s & "<br/>"
End If
Else
Response.Write "PAramName: " & s & "<br/>"
End If
i = i + 1
Next
Result:
URL: ...?Q=abc&def&P=123 produces
PAramName: Q Value: abc&def PAramName: P Value: 123
Note that this is less than robust. I am only illustrating my idea. I didn't test with no &.
It also doens't handle multiple "=" characters (if that's a possiblity as well).
If there are 2 (or more) ampersands in-between the equals, then only the last one is a parameter separator. So, using your URL above, and assuming that value1 = "abc&def", and value2 = "123", then the URL will look like:
http://myhostname/process.asp?param1=abc&def&param2=123
Notice there's 2 ampersands in-between the 2 equals. The last one will be your parameter separator, the rest are part of the value. And any ampersands after the last equals are also part of the value.
You'll have to dissect the incoming URL and apply the appropriate logic.

Classic ASP Array not returning values, error 500

I'm working on executing the same code several times to produce a table. My first thoughts went out to using an array to do this.
Here is what i have got so far:
Dim iRow
iRow = 0
'alternate color for rows
Do While Not rsGlobalWeb.EOF
If iRow Mod 2 = 0 Then
response.write "<tr bgcolor=""#FFFFFF"">"
Else
response.write "<tr bgcolor=""#EEEEEE"">"
End If
'some other code
SqlBackup = "SELECT * FROM CMDBbackup WHERE Naam_Cattools = '" & rsGlobalWeb("Device_name") & "'"
Set rsBackup = Server.CreateObject("ADODB.Recordset")
rsBackup.Open SqlBackup, dbGlobalWeb, 3
'declaration of array
Dim fieldname(5),i
fieldname(0) = "Device_name"
fieldname(1) = "Image"
fieldname(2) = "Backup"
fieldname(3) = "Uptime"
fieldname(4) = "Processor"
fieldname(5) = "Nvram"
For i = 0 to 5
If rsGlobalWeb(fieldname(i)) <> "" Then
response.write("<td>" & rsGlobalWeb(fieldname(i)) & "</td>")
Else
If Not rsBackup.EOF Then
If Not IsNull(rsBackup(fieldname(i))) And (rsBackup(fieldname(i)) <> "") Then
response.write("<td>" & rsBackup(fieldname(i)) & " (backup)</td>")
End if
Else
response.write("<td>No data found</td>")
End if
End if
Next
response.write("</tr>")
iRow = iRow + 1
rsGlobalWeb.MoveNext
Loop
The issue i have now is that the following error occurs even tho i have friendly messages turned off:
"500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed."
The logfile shows the following:
"DaEngineSDB.asp |58|800a000d|Type_mismatch 80 -" Where the 58 is the line with the Dim Fieldname.
Without the array it does show the remainder of the code (i have 1 other field which gets added). If i remove the array and fill the fieldname(i) with a normal string value it also works fine.
I was trying out stuff that google says but after attempting several things i am still running up to a wall.
Any ideas what it could be?
Thanks in advance,
Erik
First you should turn on error displaying in your iis, or read the error log for its description, google it if not sure how.
Without error description, it's way too difficult to check what is wrong.
Problem solved!
After banging my head against the wall for a day i found out that i stupidly declared the array inside the DO WHILE loop. Moved the declaration out of it and problem solved.

ASP Classic - Type mismatch: 'CInt' - Easy question

Having an issue with type conversion in ASP classic.
heres my code:
Set trainingCost = Server.CreateObject("ADODB.Recordset")
strSQL3 = "SELECT cost1 FROM tblMain WHERE (Booked = 'Booked') AND (Paid IS NULL) AND (PaidDate BETWEEN '01/04/" & startyear & "' AND '31/03/" & endyear & "')"
trainingCost.Open strSQL3, Connection
trainingCost.movefirst
totalTrainCost = 0
do while not trainingCost.eof
trainCost = trainingCost("cost1")
If NOT isNull(trainCost) then
trainCostStr = CStr(trainCost)
trainCostStr = Replace(trainCostStr, "£", "")
trainCostStr = Replace(trainCostStr, ",", "")
totalTrainCost = totalTrainCost + CInt(trainCostStr)
end if
trainingCost.movenext
loop
trainingCost.close
when I run this I get the following error:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'CInt'
/systems/RFT/v1.2/Extract.asp, line 43
which is "totalTrainCost = totalTrainCost + CInt(trainCostStr)"
Im guessing that the problem is to do with the String value being uncastable to Int in which case is there any way to catch this error? I havent worked with asp classic much so any help would be usefull
cheers
-EDIT-
the type of column cost1 is String as it may contain a number or a sequence of chars eg £10.00 or TBC
You have a couple of choices. You can be proactive by checking ahead of time whether the value is numeric using the IsNumeric function:
If IsNumeric(trainCostStr) Then
totalTrainCost = totalTrainCost + CInt(trainCostStr)
Else
' Do something appropriate
End If
...or you can be reactive by using error catching; in Classic ASP probably easiest to define a function and use On Error Resume Next:
Function ConvertToInt(val)
On Error Resume Next
ConvertToInt = CInt(val)
If Err.Number <> 0 Then
ConvertToInt = Empty
Err.Clear
End If
End Function
Or return 0 or Null or whatever suits you, then use it in your trainCost code.
Note that CInt expects an integer and will stop at the first non-digit, so "123.45" comes back as 123. Look at the other conversions, CDouble, CCur, etc.
Rather than casting to a string, why not use CCur (Cast as Currency) so that your commas and any currency symbols (I think) are effectively ignored while doing arithmetic operations?
Potentially solving the wrong problem, depends on the type of Cost1 within the database but the code is looping through the records to generate a total.
strSQL3 = "SELECT sum(cost1) FROM tblMain WHERE (Booked = 'Booked') AND (Paid IS NULL) AND (PaidDate BETWEEN '01/04/" & startyear & "' AND '31/03/" & endyear & "')"
trainingCost.Open strSQL3, Connection
etc and just read off the value as a total.
I don't see why the RS is being looped to generate a sum when the database can do that work for you. All the conversion work it has generated just looks artifical.
Heh heh. Classic ASP. You have my pity :) Anyway,
On error resume next
And then on the next line, check that it worked.
Though maybe you want CDouble. Is that a function? I can't remember.

Resources