I found this on a project I'm working on:
<% If Session("VALUE1") <> "" Then %>
document.forms[0].action= "<%=Session("VALUE1")%>";
<% Else %>
document.forms[0].action="NewPage.aspx"
<% End If %>
When I single step through this starting at the top line, the code skips over the If Session("VALUE1") but also skips over the Else as well. How is this possible?
Inside both the If and Else blocks, there's no actual server code, only markup (which happens to be javascript). Since there's nothing to execute, your debugger doesn't have anything to stop at. So it's not actually skipping both of them.
If you look at the rendered output, one of the two will end up on the page.
The code isn't skipped, it's just that you don't see the actual code that is executed.
The code that is generated for that markup when the page is compiled looks something like this:
If Session("VALUE1") <> "" Then
Response.Write(" document.forms[0].action= """)
Response.Write(Session("VALUE1"))
Response.Write(""";")
Else
Response.Write(" document.forms[0].action=""NewPage.aspx""")
End If
As the Response.Write statements are generated and has no corresponding statements in the source code, it will look like they are skipped when you single step through the code.
Related
I write the following piece of codes :
rst.Open(strSQL & Request.QueryString("C-PLACE")), conn, 0, 1
But got the following error. However, if the querystring is in English or just number, no error will pop out. Any guru can help please ?
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
/deliverable/GetMemberTest.asp, line 19
It's going to either be passing an encoding variable to the server, or in the case of your error, its saying "too few parameters". In this case, the parameter is "C-PLACE" and its suppose to be passed to your asp script from the previous page's link, something like:
/deliverable/GetMemberTest.asp?C-PLACE=THECPLACE
https://www.w3schools.com/asp/coll_querystring.asp
(citation about query strings)
or something like that .. obviously its not actually "THECPLACE", but just saying a QueryString("VARIABLENAME") looks to the URL of the previous page to pass the parameter to the script, so that error message should of done something to add a ? mark = C-PLACE= to something, and we aren't seeing that. So something on the previous page that was suppose to add this when they click a submit button didn't do it's job, or the script is just getting run on its own without the proper previous page's work being done to prepare it to execute properly on the following page.
It will also be of note that these types of things are easily hacked through sql script injection, so if you aren't validating your url first, someone could use some code to escape out of your sql and add their own code, such as one to drop your tables ..., so make sure you validate the variable FIRST instead of dumping it straight into your code. I can give some guidance into that later, but first lets figure out your problem.
(side note - can i request strSQL from you? Can you put this line in before that line:
<%
response.write("strSQL is " & StrSQL & "<BR>")
%>
All this code does is display what is stored in the StrSQL variable, see if we can figure out what is going on here. Also take note that your error message indicated that it expected 2 parameters, so we are missing 2 for this script to run properly.
EDIT - try this encoding:
<%
Response.CodePage=65001
Response.Charset="UTF-8"
Response.ContentType = "text/html"
%>
Try this strSQL, you didn't need the Response.Write and on C-PLACE you want to use '' instead of "" because the "" will exit you out of the SQL statement. Try this, and let me know how it works, but I still think we are going to need another parameter supplied to it, unless its getting one from the string and then it isn't actually counting the one supplied from the url perhaps.
<%
strSQL="SELECT * FROM DetailMemberInfo
WHERE C-PLACE=" & strSQL & Request.QueryString('C-PLACE'))"
%>
I created a .html maintenance file and I want when someone goes to the site to redirect to the maintenance folder/index.html
my code: (default.asp at the root)
<%
If InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("abc.com") ) > 0 Then
Response.Redirect("/maintenance/")
ElseIf InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("web.com") ) > 0 Then
Response.Redirect("/web/")
End If
%>
it works fine, if I go to abc.com but if I type in abc.com/blog it goes to the blog page. how do I prevent so it doesn't go to any sub folders.
Maybe using Request.ServerVariables["HTTP_HOST"] can solve your problem.
Have you tried to look in the variable Request.ServerVariables("SERVER_NAME") with a Response.Write in order to see why the check on the string fails?
Are you using this "test > do" method instead of just taking the site down and creating a custom 404.html page because:
(1) Your site receives calls via various domain names and you want some to work but not others; or
(2) You just didn't think of using the 404 method?
Anyway, if you want to do it via code, then put this at the very top (before header-very important) of a page or even use this AS your index.asp or default.asp page:
<%
s_url = Request.ServerVariables("server_name")
s_url = lcase(s_url)
b_found = InStr(1,s_url, "abc.com",1)
if (b_found <> 0) then
response.redirect("/maintenance/")
end if
%>
I want to build this page with ASP classic:
<%
Dim depart
depart = 1556
Select Case depart
Case 1
%>
<!--#include virtual="/check/noam/newDesign/test1.asp"-->
<%
Case 2
%>
<!--#include virtual="/check/noam/newDesign/test2.asp"-->
<%
Case 3
%>
<!--#include virtual="/check/noam/newDesign/test3.asp"-->
<%
Case 4
%>
<!--#include virtual="/check/noam/newDesign/test4.asp"-->
<%
Case 5
%>
<!--#include virtual="/check/noam/newDesign/test5.asp"-->
<%
Case 6
%>
<!--#include virtual="/check/noam/newDesign/test6.asp"-->
<%
Case 7
%>
<!--#include virtual="/check/noam/newDesign/test7.asp"-->
<%
Case 8
%>
<!--#include virtual="/check/noam/newDesign/test8.asp"-->
<%
End If
%>
And I like to know if the server in the background need to enter to every include or he will enter only to the include in the right case?
I need to know that because I like to know if the server will get bad performance with this one.
It really depends on what is inside the includes but I wouldn't expect such a structure to have any observable impact on performance unless you either have 100s of case statements or have 100s of requests per second for the page.
It is true that all the includes will be composed into the script first before code is executed however it should also be remember that the composed and parsed "p-code" version of the final script is cached by ASP.
If the includes are primarly just static HTML content then this approach is actually quite efficient. On the other hand the more inline global identifiers (identifiers not enclosed in Sub, Function or Class) the more time needed to register these identifiers in the Script context (however there would still need to a lot of them to make a noticable difference).
A potential alternative is to use Server.Execute instead of an include. In this case the ASP executed has its own independant script context so it can't share the callers functions and variables (this may or may not be a good thing.) Also its quite possible that Server.Execute will actually turn out slower.
Includes are loaded before they're executed in classic ASP, so you're basically loading all of these includes, even if they're not executed.
You can use a different pattern: Execute Global, which is the equivalent of using eval() in VB. You read in the file as a string, then execute it. This gets around a cluster of includes but is rather ugly in itself.
You can use server.execute
server.execute("/check/noam/newDesign/test"&depart&".asp")
OR
page="/check/noam/newDesign/test"&depart&".asp"
server.execute(page)
I would like to add data into database using if statement which based on certain condition..but my codes below doesn't work..need your advise.
<% If (rs_view.Fields.Item("CGPAOverall").Value>="2.00") Then %>
rs_view("Status")="Proceed"
<% Else %>
rs_view("Status")="Stop"
<% End If %>
I would like to save the result direct into database. How can I do that? Can't get the right codes for this. Hope you can help. Thanks.
I think you need to remove all of your "<%" and "%>" except for the first and last one.
<% If (rs_view.Fields.Item("CGPAOverall").Value>="2.00") Then
rs_view("Status")="Proceed"
Else
rs_view("Status")="Stop"
End If %>
If you are adding a new record, add a statement before you try to set the value of your fields
rs_view.AddNew()
rs_view("Status")="Stop"
rs_view.Update()
If you are editing an existing record, you need to call rs_view.Update() after you have set the values for your fields, you wish to change the value of.
You need a call to rs_view.Update(), to push the changes to the DB.
<% If (rs_view.Fields.Item("CGPAOverall").Value>="2.00") Then
rs_view("Status")="Proceed"
Else
rs_view("Status")="Stop"
End If
rs_view.Update() %>
Statements enclosed between <% %> will be executed on the server (IIS). I suggest, you read something on difference between server side code execution & the need/usage of <% %> and when to use it/not to use it.
Without a given error message it is hard to know where the problem is. Try this:
I assume your CGPAOverall column is stored in a numeric column. For this purpose it is safe to convert to double and compare against a number instead of a string.
<%
If cdbl(rs_view.Fields.Item("CGPAOverall").Value) >= 2 Then
rs_view("Status")="Proceed"
Else
rs_view("Status")="Stop"
End If
%>
It's impossible to solve your problem without telling us the error message.
One possibility: I see your recordset is called "rs_view." Not sure if "rs_view" is coming from a view, table, or stored procedure... but if it's coming from a view, not all views are updateable.
(Views are essentially canned select statements created from one or more tables and can contain various calculated columns. You can't save changes to those calculated columns back to the database because they're not actually columns in any table)
I have moved an sql database from one server to a new one (detached/attached)
Now i experience some strange behavior as it does not work but NO error is displayed.
This is the code
<%
const database_dsn="PROVIDER=SQLNCLI10; SERVER=FR-2626\SQLLOP;DATABASE=Lop;Uid=admin-sql;Pwd=xxxx;"
response.write "Step 0//"
set conn=server.CreateObject("ADODB.Connection")
set RS=server.CreateObject("ADODB.Recordset")
conn.Open database_dsn
response.write "Step 1//"
req = "Select count(*) From tblArticleList"
response.write "Step 2//"
set RS = conn.Execute(req)
response.write "Step 3//"
%>
The program stops at Step 2; then nothing, no error is displayed...
I just don t know what to do..How can i get some error?
Thanks
Jonathan
Oh i partially found the answer for the error display.
In the Debug pane of the IIS directory configuration, Enable ASP debugging should NOT be checked...althought i thougth it should...
have you got your browser set to "show friendly http errors" this in conjuction with what you have already identified is the common causes I've had for not seeing an error message.
Shahkaplesh is also right that you can use Server.GetLastError() to get the last error that occurred but you shouldn't need to do this in this example.
When executing a query you don't use the "Set" command. I don't know why its not showing you anything, but your code should look more like this:
<%
const database_dsn="PROVIDER=SQLNCLI10; SERVER=FR-2626\SQLLOP;DATABASE=Lop;Uid=admin-sql;Pwd=xxxx;"
response.write("Step 0//")
set conn=server.CreateObject("ADODB.Connection")
set RS=server.CreateObject("ADODB.Recordset")
conn.Open database_dsn
response.write("Step 1//")
req = "Select count(*) From tblArticleList"
response.write("Step 2//")
RS = conn.Execute(req)
response.write("Step 3//")
%>
Yes, the parentheses on the "Response.Write" are optional. But I'm OCD like that and it makes troubleshooting a little easier.
You need to place error checking code to find out what the actual error might be.
I would suggest that you change you code like so:
<%
const database_dsn="PROVIDER=SQLNCLI10; SERVER=FR-2626\SQLLOP;DATABASE=Lop;Uid=admin- sql;Pwd=xxxx;"
'Its very important to add this line!!! '
On Error Resume Next
'Its very important to add this line!!! '
response.write "Step 0//"
set conn=server.CreateObject("ADODB.Connection")
set RS=server.CreateObject("ADODB.Recordset")
conn.Open database_dsn
if err.number<>0 then
response.write err.description
end if
response.write "Step 1//"
req = "Select count(*) From tblArticleList"
response.write "Step 2//"
set RS = conn.Execute(req)
if err.number<>0 then
response.write err.description
end if
response.write "Step 3//"
%>
And not to kick a dead horse but I'm doing something similar against an Oracle database and I've had two phantom problems I have yet to identify root cause but here's two things that made them go away.
1. Name all columns in a Query and Alias any that are calculated (sum, count, avg, etc.) So your query would become
req = "Select count(*) NumRows From tblArticleList"
2. Wrapping my query string in a call to cstr caused the result.EOF flag to be populated correctly rather than be returned with an empty or null value causing a simple DO WHILE NOT result.EOF Some Action LOOP to create an infinite loop until the web request timed out. So e.g.
response.write "Step 2//"
set RS = conn.Execute(cstr(req))
Nothing major just a couple tips if you get stuck and can't find out why. Follow the debugging advice above though, that's good info.
I think Server has a GetLastError method, which you can check to find out what error occurred while running any statement.
e.g. On error resume next
.... statement that could cause an error....
errorObject = Server.GetLastError()
For ASPError object, refer http://www.w3schools.com/asp/asp_ref_error.asp
Actually don't mean to be disagreeable, but yes you do need a Set there, as you are setting an object type and presumably wanting to use the return value at some point (if this wasn't just a test script which it looks like to me).
Also btw parentheses are not really correct there in Response.Write() as it does not return a value. They only happen to work on single parameter subs because you can put parentheses anywhere you like around expressions.
eg:
a = (b)
Response.Write ((("test"))&(1))