How to do a if else statement in classic asp - asp.net

I'm new to classic asp and I'm trying to figure out this simple if else statement.
For some reason it's just recognizing person 2 and not even trying person 1?
Any idea on how to fix? Thanks
This is my code:
<%
Dim GetPath
GetPath = request.ServerVariables("URL") & query_string
Dim page
page = "/products/dowlex/index.htm"
if GetPath = page then
varrecipient = "email1#email.com"
Response.Write("*Path = " & GetPath)
Response.Write("Person 1")
else
varrecipient = "email2#email.com"
Response.Write("*Path = " & GetPath)
Response.Write("Person 2")
end if
varFormName = "Contact"
varRHBusinessUnit = "businessname"
varLanguage = "ENGLISH"
varCourtesyResponse = "Y"
varRedirect = "#noredir?formRun=true"
varSubject = "Ask an Expert Form"
%>

I would compare the two strings based on the same case...
if UCase(GetPath) = UCase(page) then
And of course, if query_string ever has a value, then the 1st case will never be true.

The formatting of your statement is fine. If... Then... Else... End if.
I would do a Response.Write("GetPath") to see if you are getting back, what you think you should be.

I have a couple thoughts.
1) Can you use Response.Write to display what's in "GetPath" before the if statement? That might help you see what's going wrong!
2) Try changing the variable names. The editor is making "GetPath" blue, as though it's a reserved word. That might be messing things up.

I'm sorry guys, I messed up a small code and thats why it wasn't working.
I forgot to complete the full path of the site. Thanks for all your help and suggestions!
*Path = /plasticpipes/eu/products/dowlex/index.htm
*Page = /products/dowlex/index.htm

Related

Got this error "Comma, ')', or a valid expression continuation expected"

can anyone help me to solve this error. Got error at line
wtp.Add(New WaterTreament() With {.position = positionName, .value = Convert.ToDouble(sqlRs2("value").ToString()), .timestamp = sqlRs2("dtimestamp").ToString()})
Here's the code
Dim wtp As New List(Of WaterTreament)()
sqlRs2.Open(strSQL2, objConn2)
If Not sqlRs2.EOF Then
Dim positionName = sqlRs2("position").ToString()
If (positionName = "60") Then
positionName = "CHLORINE"
ElseIf (positionName = "61") Then
positionName = "TURBIDITY"
ElseIf (positionName = "62") Then
positionName = "PH"
ElseIf (positionName = "63") Then
positionName = "FLORIDE"
End If
wtp.Add(New WaterTreament() With {.position = positionName, .value = Convert.ToDouble(sqlRs2("value").ToString()), .timestamp = sqlRs2("dtimestamp").ToString()})
End If
Well, sometimes it is REALLY a lot better to just write out a few extra lines of code. I mean, fancy pants can be cool, and great, but often a few extra lines of code is a bettter approach.
So, say try this:
dim MyWaterTreat as new WaterTrament
MyWaterTreat.position = positionName
MyWaterTreat.value = Convert.ToDouble(sqlRs2("Value).ToString())
MyWaterTreat.TimeStamp = sqlRs2("dtimesamp").ToString()
wtp.Add(MyWaterTreat)
You get much nicer intel-sense, and probably save a pot of coffee in the debugging process anyway.
So, sometimes, you better off to write a few extra lines of code. And with VS, then say ctr-d to duplicate the previous line? You might result in a bit more code but you the developer did not actually have to type in that code, or all that much of it.
So, create a new instance of the class. Add the values along with GREAT nice intel-sense, and then add that new object to the list.
Better yet, you also tend to get intel-sense for miss matched data types.

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.

Writing an array as a comma separated list to screen

Now it seems like a really simple question and I may just be being thick, but what I'm trying to achieve is basically print an array to screen in the following format:
Item 1, Item 2, Item 3, Item 4
Although I say to screen, as that was the best way I could describe it, I'm actually writing it to the page inside some Javascript.
The way I'm currently going about writing it out is as follows:
for each b in theDates
Response.Write("'" + b.CallDate + "',")
next
But obviously that returns a string of Item 1, Item 2, Item 3, Item 4,.
Is there a simple way of getting rid of the last comma or am I going about this completely wrong?
You could use String.Join method. Not sure about VB but something like that:
Response.Write(String.Join(", ", theDates.Select(Function(n) "'" & n.CallDate & "'").ToArray()))
Try String.Join (documentation). My VB is rusty, but hopefully you can read this C#:
var datesInQuotes = theDates.Select(date => "'" + date.CallDate + "'");
Response.Write(String.Join(", ", datesInQuotes));
As others have said, String.Join is what you want in this case.
More generically: you've got to either detect when you're at the the last element and not append a comma, or detect when you're at the first element and not prepend a comma.
Typically it's easier to detect when you're at the first, so:
dim firstDone as bool = false
for each b in theDates
if firstDone then Response.Write (",")
Response.Write("'" + b.CallDate + "'")
firstDone = true
next
(Excuse my rusty vb)
There's no need to String.join this.
st="'"
for each b in theDates
Response.Write(st + b.CallDate + "'")
st=", '"
next
No, you aren't wrong - totally.
Loop through theDates and put the data into a string.
After you are done looping, remove the last comma:
myString.Remove(myString.Length - 1, 1);
Response.Write(myString)
A side note: You really shouldn't output to the screen with Response.Write. You should set the text of a server control as your output. A label is perfectly fine. You will notice that your text won't appear where you think it will if you don't.
You could write the comma in front of each item except the first one.
I would recommend to do just as you were doing, and then use the Response.Remove(s.Length-1, 1)
In C#:
for (int i = 0; i < theDates.Length; i++)
{
if (i > 0)
{
Response.Write(",");
}
Response.Write(theDates[i]);
}
all too complicated. Keep it simple
dim st as string ="'"
for each b in theDates
Response.Write(st + b.CallDate + "'")
st=", '"
next
response.write (st.TrimEnd(New Char() {" ", ","})

Detecting null/empty input from user

How can I check if the user has input a null or empty string in classic-asp? Right now I am have this code.
If Request.Form("productId") == "" Then
'my code here
End If
But its not working.
Classic ASP/VBScript uses one = to check for equality, not two. Another thing you may want to try is
If Request.Form("productid") = "" Then
Code here
End If
It is a mess. Here's what I have found ...
(1) To look for existence in the QS, use if IsEmpty(x)=false (ie, URL?x)
(2) To look for a value in the QS, look for if x <> "" (ie, URL?x=anything)
Good luck!
If IsEmpty(Request.Form("inputPhoneNo")) = False Then
response.Write"<script language=javaScript>alert('Blank Phone Number');</script>"
response.Write"<script language=javascript>history.back()</script>"
Else
End If

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