Error with FormatDateTime in VBscript - asp-classic

I am a running VB script with asp classic and I am getting the following error:
Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument: 'FormatDateTime'
/whatsnew/updated_pages_www.htm, line 52
I am trying to work out what is causing the error. Is there anything wrong with the format of the date in the csv file?
The date format is: 20090220122443
PAGE CODE BELOW:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Response.CharSet = "UTF-8" %>
<%
pagetitle="What was published last week on casa.gov.au"
%>
<%
connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("/whatsnew/data")
set connect = Server.CreateObject("ADODB.connection")
connect.open connectString
selectSQL = "SELECT * FROM www.csv"
set www_RS = connect.execute(selectSQL)
%>
<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"-->
<p class="breadCrm">Home <span>></span> What's New</p>
<!--<img src="/_lib/images/menu/yourarea.gif" alt="" width="891" /> -->
<div class="twoColumnRow">
<div class="twoColumnContent">
<div class="contentPad">
<!-- Start of main content -->
<p class="imageRight"> </p>
<h1><%=pagetitle%></h1>
<%
www_RS.MoveFirst
%>
<caption><h3>New or Amended on www.casa.gov.au</h3></caption>
<ul class="relatedInfoLinks">
<%
Dim pagecode, pagecode2, newfiledate, publisheddate, moddate, createddate, newfilediff, recently_published
pagecode = www_RS("PAGECODE").Value
While not www_RS.EOF
pagecode = www_RS("PAGECODE").Value
pagecode2 = Instr(pagecode,"PC_")
pagecode3 = Instr(pagecode,"~")
createddate = FormatDateTime(www_RS("CREATED_DATE").Value,1)
moddate = FormatDateTime(www_RS("MOD_DATE").Value,1)
publisheddate = FormatDateTime(www_RS("PUB_DATE").Value,1)
newfilediff = DateDiff("y",www_RS("CREATED_DATE").Value,www_RS("PUB_DATE").Value)
recently_published = DateDiff("y",www_RS("PUB_DATE").Value,dtNow)
if (pagecode2 = 1) and (pagecode3 = 0) and (recently_published < 8) then
%>
<li>
<%
Response.Write("<a href='http://casa.gov.au/scripts/nc.dll?WCMS:STANDARD::pc=" & pagecode & "'>" & www_RS("DESCRIPTION").Value & "</a>")
%>
<BR>
Last modified <%=publisheddate%>
<BR>
<%
if newfilediff < 8 then
%>
<span style="color:red">* This is a new page</span>
<%
end if
%>
</li>
<BR>
<%
end if
www_RS.MoveNext
Wend
%>
</ul>
<!-- End of main content -->
</div> <!-- end contentPad div -->
</div> <!-- end twocolumncontent div -->
<div class="twoColumnLinks">
<!--#INCLUDE VIRTUAL="/_lib/include/quicklinks.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/mylinks.htm"-->
</div> <!-- end twocolumnlinks div -->
</div> <!-- end twocolumnrow div -->
<!--#INCLUDE VIRTUAL="/_lib/include/footer.htm"-->

The FormatDateTime function requires a validly formatted date as the first parameter.
Depending on the date/time format used in your location, it will be something like this (example: US date format):
"02-20-2009 11:24:43 AM"
"02/20/2009 11:24:43 AM"
"02-20-2009 14:24:43"
"02/20/2009 14:24:43"
As a test, you can check the validity of a date like this:
d = CDate("02-20-2009 11:24:43 AM")
d = CDate("02/20/2009 11:24:43 AM")
d = CDate("02-20-2009 14:24:43")
d = CDate("02/20/2009 14:24:43")
Or, using IsDate:
b = IsDate("02-20-2009 11:24:43 AM")
b = IsDate("02/20/2009 11:24:43 AM")
b = IsDate("02-20-2009 14:24:43")
b = IsDate("02/20/2009 14:24:43")
In your case, your date string: "20090220122443" is a valid date/time, but it is not in a valid date/time format.
You could use a function to convert your date string into a valid format. Here is an example of some code to do the conversion.
strDate = "20090220122443"
wscript.echo "strConvertDateString(strDate) =" & strConvertDateString(strDate)
wscript.echo "dateConvertDateString(strDate)=" & dateConvertDateString(strDate)
wscript.echo
wscript.echo FormatDateTime(strConvertDateString(strDate),1)
wscript.echo FormatDateTime(dateConvertDateString(strDate),1)
wscript.echo
Function strConvertDateString (strDateString)
strConvertDateString = mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2)
End Function
Function dateConvertDateString (strDateString)
dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
End Function
The function strConvertDateString accepts a date/time string in your format and returns a string in a format acceptable to the FormatDateTime function.
The function dateConvertDateString accepts a date/time string in your format and returns a date (CDate), which is also acceptable to the FormatDateTime function.
The first one should work in most locations. The second might work better if there are issues with date conversions particular to your location. You should only need to implement and use one of these two functions.
In any case, depending on your location, you may need to edit the functions to adjust the way that mid() is used to compose the date/time string.
Note: This was written for testing in VBScript (cscript.exe). Copy/cut/paste the Function into your .asp file for use. Then use the function like this:
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
Edit:
Here is a more detailed explaination of how to add this function to your .asp page and how to use the function.
First, you need to add the function to your ASP page.
Edit your page.
Normally, function declarations are placed inside the <head> section like this:
<html>
<head>
...
...
<%
Function dateConvertDateString (strDateString)
dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
End Function
%>
</head>
<body>
...
<!-- there will be more <html> or <% asp code %> here ... -->
Or, inside the <body> section like this:
<html>
<head>
...
...
</head>
<body>
<%
Function dateConvertDateString (strDateString)
dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
End Function
%>
...
<!-- there will be more <html> or <% asp code %> here ... -->
In your case, it looks like the sections of the page that contain the <html>, <head>, </head>, and <body> tags could be contained in the included files. You can verify that by examining the files:
"/_lib/include/header.htm"
"/_lib/include/menu.htm"
So, in your case, you'll want to insert the function declaration into your page after the <!--#INCLUDE VIRTUAL= lines, like:
<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"-->
<%
Function dateConvertDateString (strDateString)
dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
End Function
%>
Or, you could probably insert the function declaration into one of these files:
"/_lib/include/header.htm"
"/_lib/include/menu.htm"
Next, find the statements that are using VBScript date functions where you are currently passing dates formatted like this: "20090220122443"
That would be (most likely) all of these:
createddate = FormatDateTime(www_RS("CREATED_DATE").Value,1)
moddate = FormatDateTime(www_RS("MOD_DATE").Value,1)
publisheddate = FormatDateTime(www_RS("PUB_DATE").Value,1)
newfilediff = DateDiff("y",www_RS("CREATED_DATE").Value,www_RS("PUB_DATE").Value)
recently_published = DateDiff("y",www_RS("PUB_DATE").Value,dtNow)
and edit those statements to use the function to convert the dates, like this:
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
moddate = FormatDateTime(dateConvertDateString(www_RS("MOD_DATE").Value),1)
publisheddate = FormatDateTime(dateConvertDateString(www_RS("PUB_DATE").Value),1)
newfilediff = DateDiff("y",dateConvertDateString(www_RS("CREATED_DATE").Value),dateConvertDateString(www_RS("PUB_DATE").Value))
recently_published = DateDiff("y",dateConvertDateString(www_RS("PUB_DATE").Value),dtNow)
So, if the code you provided in your question is still correct and complete, here is the same code using the date conversion function:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Response.CharSet = "UTF-8" %>
<%
pagetitle="What was published last week on casa.gov.au"
%>
<%
connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("/whatsnew/data")
set connect = Server.CreateObject("ADODB.connection")
connect.open connectString
selectSQL = "SELECT * FROM www.csv"
set www_RS = connect.execute(selectSQL)
%>
<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"-->
<%
Function dateConvertDateString (strDateString)
dateConvertDateString = CDate(mid(strDateString,5,2) & "/" & mid(strDateString,7,2) & "/" & mid(strDateString,1,4) & " " & mid(strDateString,9,2) & ":" & mid(strDateString,11,2) & ":" & mid(strDateString,13,2))
End Function
%>
<p class="breadCrm">Home <span>></span> What's New</p>
<!--<img src="/_lib/images/menu/yourarea.gif" alt="" width="891" /> -->
<div class="twoColumnRow">
<div class="twoColumnContent">
<div class="contentPad">
<!-- Start of main content -->
<p class="imageRight"> </p>
<h1><%=pagetitle%></h1>
<%
www_RS.MoveFirst
%>
<caption><h3>New or Amended on www.casa.gov.au</h3></caption>
<ul class="relatedInfoLinks">
<%
Dim pagecode, pagecode2, newfiledate, publisheddate, moddate, createddate, newfilediff, recently_published
pagecode = www_RS("PAGECODE").Value
While not www_RS.EOF
pagecode = www_RS("PAGECODE").Value
pagecode2 = Instr(pagecode,"PC_")
pagecode3 = Instr(pagecode,"~")
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
createddate = FormatDateTime(dateConvertDateString(www_RS("CREATED_DATE").Value),1)
moddate = FormatDateTime(dateConvertDateString(www_RS("MOD_DATE").Value),1)
publisheddate = FormatDateTime(dateConvertDateString(www_RS("PUB_DATE").Value),1)
newfilediff = DateDiff("y",dateConvertDateString(www_RS("CREATED_DATE").Value),dateConvertDateString(www_RS("PUB_DATE").Value))
recently_published = DateDiff("y",dateConvertDateString(www_RS("PUB_DATE").Value),dtNow)
if (pagecode2 = 1) and (pagecode3 = 0) and (recently_published < 8) then
%>
<li>
<%
Response.Write("<a href='http://casa.gov.au/scripts/nc.dll?WCMS:STANDARD::pc=" & pagecode & "'>" & www_RS("DESCRIPTION").Value & "</a>")
%>
<BR>
Last modified <%=publisheddate%>
<BR>
<%
if newfilediff < 8 then
%>
<span style="color:red">* This is a new page</span>
<%
end if
%>
</li>
<BR>
<%
end if
www_RS.MoveNext
Wend
%>
</ul>
<!-- End of main content -->
</div> <!-- end contentPad div -->
</div> <!-- end twocolumncontent div -->
<div class="twoColumnLinks">
<!--#INCLUDE VIRTUAL="/_lib/include/quicklinks.htm"-->
<!--#INCLUDE VIRTUAL="/_lib/include/mylinks.htm"-->
</div> <!-- end twocolumnlinks div -->
</div> <!-- end twocolumnrow div -->
<!--#INCLUDE VIRTUAL="/_lib/include/footer.htm"-->
To give this a try, copy your current .htm file to something like whatever.htm. Then, replace (or edit) your current .htm with the code above.
Unfortunately, I don't have a way to test your complete ASP page.
If you still have problems with this, it would be very helpful if you could provide a copy of your actual .htm file, and a copy of the files: /_lib/include/header.htm, and /_lib/include/menu.htm

Related

Removal of <% %> and %><% in Classic ASP when there is nothing between them causes the web page to crash. Why is that?

I am working with Classic ASP and I have a need to make the code simpler. In an effort to do what is similar to what we see here: Auto-populating Select Field via jQuery's Ajax where we use ajax to populate a select filed, we load the contents of a area with by using a separate asp file to load. I assume that the loaded file is free of <% markings. While testing the commands contained in that file I am in the process of removing those marks. Why would removing a %><% mark (where it is just a close followed by an open) throw an error? And why would it be necessary to have something like %>"<% where it is just one character?
The reason why I posted an image of the code block was because the %> and %> symbols were highlighted by color in such a way as to better visualize what was going on.
Here is the code block:
<%
Function FunctionName(name, selection)
%>
<select name = "<%= name%>"><%
Set RTConn = Server.CreateObject("ADODB.Connection")
RTConn.Open("Provider=SQLOLEDB;Password=three4me;Persist Security Info=True;User ID=sa;Initial Catalog=DATABASE;Data Source=SERVER")
Set RT = Server.CreateObject("ADODB.Recordset")
sqlQuery = "SELECT DISTINCT id, Replace(Name, ' ', ' ') AS Name, Num, Address, City, State FROM RedactedTablename WHERE active = 1 OR ID = '" & selection & "' ORDER BY Replace(Name, ' ', ' '), State, City, Num"
RT.Open sqlQuery, RTConn, 3, 3
Do While Not RT.EOF
response.write "<option value=" & RT.Fields("id")
%>" <%
if cstr(RT.Fields("id")) = selection then
response.write " selected "
elseif (selection = "" OR selection = "0") AND trim(RT.Fields("Name")) = "NA" then
response.write " selected "
end if
%>><%=RT.Fields("Name")%><%
if not RT.Fields("Name") = "NA" AND not RT.Fields("Name") = "NA" then
response.write " (" & RT.Fields("City") & ", " & RT.Fields("State") & ") - " & RT.Fields("Num")
end if
%>
response.write"</option>"
<%
RT.MoveNext
Loop
RT.Close
RTConn.Close %>
</select>
<%
End function
%>
The end tag that was removed was paired with a start tag of <%= not <% which has caused the syntax error.
The reason is <%= is a shorthand form of Response.Write and has to be paired with a closing %> tag.
Acceptable:
<%= link_label %>
Invalid syntax:
<%= link_label
Also, there are other issues with the code, for example, #Flakes pointed out in the comments that response.write"</option>" is not located within Classic ASP preprocessor tags (<% and %>).
While this won't cause a syntax error it will cause the line to be interpreted as HTML and will be output to the client as is.

How do I write this IF statement within my ASP page?

I'm trying to display an image on website only if a value in the database is true. I'm using the below code as a template (which is currently working) as my guide, though mine is simpler. Any help would be greatly appreciated.
<% strSQL4 = valid SQL statement
set r4 = d2.execute(strSQL4)
if (r4.EOF = False) and (r4.BOF = False) then
else
r4.moveFirst
while (r4.EOF = False) and (r4.BOF = False) %>
<li><%= r4("Database Field") %></li>
<% r4.movenext
wend
end if %>
That is the code I'm basically emulating, but I'm just trying to display an image if a bool variable is true in a database, per my code below:
<%# ACTLBool = "SELECT ACTL FROM ATTORNEYS WHERE ATTY_ID = " & AttorneyID
if (ACTLBool = True) then %>
<div id="ACTLDiv"><img id="ACTLLogo" src="img/ACTL.jpg" alt="ACTL Logo" /> </div>
<%# else end if %>
I don't need it to do anything if the ACTLBool is false. Any ideas?
assuming conn is your adodb.connection object
Dim rs : set rs = conn.execute("SELECT count(ACTL) as c FROM ATTORNEYS WHERE ATTY_ID = " & CLng(AttorneyId))
If rs("c") > 0 Then
response.write "<div id='ACTLDiv'><img id='ACTLLogo' src='img/ACTL.jpg' alt='ACTL Logo' /> </div>"
End If
Set rs = Nothing

need to implement recaptcha in .asp page

anyone please know how to add recaptcha in .asp , i am having public and privte keys
<TD align="middle">Security Code: </TD>
<TD align="middle">
<script type="text/javascript"
src="http://www.google.com/recaptcha/api/challenge?k=01M2eMS32Xs6oGtCaBu3AkHQ==">
</script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=01M2eMS32Xs6oGtCaBu3AkHQ=="
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
</TD>
Using reCAPTCHA with Classic ASP
The reCAPTCHA ASP guide provides a simple way to place a CAPTCHA on your ASP page, helping you stop bots from abusing it. The code below wraps the reCAPTCHA API.
After you've signed up for your API keys, you can add reCAPTCHA to your classic ASP site by pasting the code below at the top of your ASP page:
<%
recaptcha_challenge_field = Request("recaptcha_challenge_field")
recaptcha_response_field = Request("recaptcha_response_field")
recaptcha_public_key = "your_public_key" ' your public key
recaptcha_private_key = "your_private_key" ' your private key
' returns the HTML for the widget
function recaptcha_challenge_writer()
recaptcha_challenge_writer = _
"<script type=""text/javascript"">" & _
"var RecaptchaOptions = {" & _
" theme : 'red'," & _
" tabindex : 0" & _
"};" & _
"</script>" & _
"<script type=""text/javascript"" src=""http://www.google.com/recaptcha/api/challenge?k=" & recaptcha_public_key & """></script>" & _
"<noscript>" & _
"<iframe src=""http://www.google.com/recaptcha/api/noscript?k=" & recaptcha_public_key & """ frameborder=""1""></iframe><>" & _
"<textarea name=""recaptcha_challenge_field"" rows=""3"" cols=""40""></textarea>" & _
"<input type=""hidden"" name=""recaptcha_response_field""value=""manual_challenge"">" & _
"</noscript>"
end function
' returns "" if correct, otherwise it returns the error response
function recaptcha_confirm(rechallenge,reresponse)
Dim VarString
VarString = _
"privatekey=" & recaptcha_private_key & _
"&remoteip=" & Request.ServerVariables("REMOTE_ADDR") & _
"&challenge=" & rechallenge & _
"&response=" & reresponse
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", "http://www.google.com/recaptcha/api/verify", False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send VarString
Dim ResponseString
ResponseString = split(objXmlHttp.responseText, vblf)
Set objXmlHttp = Nothing
if ResponseString(0) = "true" then
'They answered correctly
recaptcha_confirm = ""
else
'They answered incorrectly
recaptcha_confirm = ResponseString(1)
end if
end function
server_response = ""
newCaptcha = True
if (recaptcha_challenge_field <> "" or recaptcha_response_field <> "") then
server_response = recaptcha_confirm(recaptcha_challenge_field, recaptcha_response_field)
newCaptcha = False
end if
%>
What happens here is the variables server_response and newCaptcha are set whenever the page is loaded, allowing you to determine the state of your page.
You can use the following HTML as a skeleton:
<html>
<body>
<% if server_response <> "" or newCaptcha then %>
<% if newCaptcha = False then %>
<!-- An error occurred -->
Wrong!
<% end if %>
<!-- Generating the form -->
<form action="recaptcha.asp" method="post">
<%=recaptcha_challenge_writer()%>
</form>
<% else %>
<!-- The solution was correct -->
Correct!
<%end if%>
</body>
</html>
As shown here:
http://developers.google.com/recaptcha/docs/asp

How to execute code from dynamic generated button

i try to make a gui (hta) which can install on a client, one or more printers from the printer server.
the problem is when i create the button "install", the function is executed on form load and not by clicking on the button.
i don't understand why. can you help me please?
<HTML>
<HEAD>
<TITLE>printer installation</TITLE>
<HTA:APPLICATION ID = 'AppBase'>
<script language="VBScript">
Dim WshNetwork, objPrinter, intDrive, intNetLetter
strComputer = "change_printer_server_name_or_ip"
strHTML = "<TABLE BORDER='0'>"
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
strHTML = strHTML & "<TR><TD><input type='button' value='install printer' name='" & objPrinter.Name & "' onclick='" & installprinter(printname, printserver) &"'>" & objPrinter.Name & "</input></TD><TD>" & objPrinter.Location & "</TD><TD>" & objPrinter.portname & "</TD></TR>"
next
strHTML = strHTML & "</TABLE>"
DataArea.InnerHTML = strHTML
function installprinter(printname, printserver)
Set WshNetwork = CreateObject ("WScript.Network")
on error resume next
PrinterPath = "\\" & printserver & "\" & printname
WshNetwork.RemovePrinterConnection PrinterPath, true, true
WshNetwork.AddwindowsPrinterConnection (PrinterPath)
msgBox "L'imprimante a été installée avec succès"
end function
</script>
</HEAD>
<BODY>
<span id="DataArea"></span>
</BODY>
The load order of your page is important.
When you attempt to assign DataArea, the DOM is not yet initialised, so DataArea does not exist. Here is a simple example of how to invoke your code when the DOM is ready:
<html>
<head>
<title>printer installation</title>
<HTA:APPLICATION ID = 'AppBase'>
<script language="VBScript">
strHTML = "<TABLE BORDER='0'>"
strHTML = strHTML & "<tr><td>Testing</td></tr>"
strHTML = strHTML & "</TABLE>"
Function OnLoad()
DataArea.InnerHTML = strHTML
End Function
</script>
</head>
<body onload="OnLoad">
<span id="DataArea"></span>
</body>
</html>
Note the <body onload="OnLoad"> and the new Function OnLoad().
EDIT - Updated to include a function invoked from the generated html:
<html>
<head>
<title>printer installation</title>
<HTA:APPLICATION ID = 'AppBase'>
<script language="VBScript">
strHTML = "<TABLE BORDER='0'>"
strHTML = strHTML & "<tr><td><button onclick=""TestingFunction"">Testing</button></td></tr>"
strHTML = strHTML & "</TABLE>"
Function OnLoad()
DataArea.InnerHTML = strHTML
End Function
Function TestingFunction()
MsgBox("Hello")
End Function
</script>
</head>
<body onload="OnLoad">
<span id="DataArea"></span>
</body>
</html>
You are actually calling your installprinter routine when generating your HTML. Instead you need to format the HTML to be a valid call at the time the button is clicked. You want your eventual HTML to look like this:
<TR><TD><input type='button' value='install printer' name='PrinterName'
onclick='installprinter "PrinterName", "PCName"'>PrinterName</input></TD>
<TD>PrinterLoc</TD>
<TD>PortName</TD></TR>
Note that since you're calling a sub, you exclude the parentheses. It looks weird but that's the way VBScript works. So your code to generate a table row should go like this instead:
strHTML = strHTML & "<TR><TD><input type='button' value='install printer' name='" & _
objPrinter.Name & "' onclick='installprinter """ & objPrinter.Name & """, """ & _
strComputer & """'>" & objPrinter.Name & "</input></TD><TD>" & _
objPrinter.Location & "</TD><TD>" & objPrinter.portname & "</TD></TR>"

little help on asp - response write

I set a variable :
Dim adoRecordset
and set it as:
Set adoRecordSet = Server.CreateObject("ADODB.RecordSet")
adoRecordset.Open mSQL , adoConnection , adOpenForwardOnly
and I use it into show my data from database
eg. 1. <td align="left"><%=adoRecordset("loc")%></td>
and I would like add a asp code "if" & "else"
but this : 2. Response.Write "<td adoRecordset(loc)></td>"
doesn't work.
How can I make asp code 2. work as 1. ?
My asp classic is rusty, but I think you are looking for something like:
<%
If adoRecordset("loc") <> "" Then
Response.Write(adoRecordset("loc"))
End If
%>
Or with a local var to cache the result:
<%
Dim someLoc
Set someLoc = adoRecordset("loc")
If someLoc <> "" Then
Response.Write("<td>" & someLoc & "</td>")
End If
%>
If you've got large amounts of conditional Html output, then you can switch out of server code again, like so:
<% If something or other Then %>
Some Conditional Html Here
<% Else %>
Else Html here
<% End If %>
<%= is shorthand to emit a value
whereas <% escapes to server side code.
Dim adoRecordset
Set adoRecordSet = Server.CreateObject("ADODB.RecordSet")
adoRecordset.Open mSQL , adoConnection , adOpenForwardOnly
if not adoRecordset.EOF then
do while not adoRecordSet.EOF
if adoRecordset("columnName") = "test value 1" then
response.write("<td>" & adoRecordset("columnName") & "</td>")
else
response.write("<td>I want to do something else here</td>")
end if
adoRecordset.movenext
loop
end if
adoRecordset.close
set adoRecordset = nothing

Resources