If PageCount ist reached, start from First Page ( ASP Classic ) - asp-classic

I have an Active Server Page, which displays Booking of the current Day. I setted the PageSize to two, so my display is displaying just 2 bookings per side, if there are more Records. So actually i have 8 bookings in my Recordset, so my ASP creates 4 Pages.
I wrote the following function:
Function getNext10(num)
getNext10 = CurrPage + 1
End Function
Finally i call that function in a meta tag, to automatically change the pages:
<meta http-equiv="refresh" content="10;URL=paging.asp?PageNo=<% Response.Write(getNext10(CurrPage))%>" />
It is working like charm.
But i have just one more Problem. If i do that like this the PageNo is increment endless.
My PageCount is 4.
So what i need in my function is a logic which checks whether the PageCount has been reached or not. If yes then he should start from the first page again, if not then increment until pagecount has reached.
Can someone help me with that? Thanks!!
EDIT:
I Wrote that function:
Function getNext10(num)
getNext10 = num
if getNext10 < i then // In `i`, i have my pagecount (4), which i got from Recordset.PageCount
// I checked it with Response.Write()
getNext10 = CurrPage + 1
End if
End Function
If i use i the if clause is not working, i dont know why. Its only working if i use directly a number.

If you know the page number will always be 4, then you can make a check for that.
Function getNext10(num)
if (num < i) then
CurrPage = num + 1
else
CurrPage = 1 'Reset the page count
end if
'Updating the variable used to call the page iterator
getNext10 = CurrPage
End Function
If you do not always know the page number then you will need to calculate the number of pages somehow to check against.

Related

Trouble with "string.contains" function

I am writing a web form that has a unique travelID per travel request. (see blank column in picture below, I'm not high enough reputation to embed the picture in here)
Some of the travel requests have attached documents and others do not. I wrote this code:
Dim dirs As String() = Directory.GetFiles("E:\DomainWebs\Intranet\fileups\TravelDocs\")
Dim i As Integer = 0
For Each GridRecord As GridRecord In TravelWebDataGrid.Rows
For Each doc As String In dirs
If doc.Contains(TravelWebDataGrid.Rows(i).Items(10).Value) = True Then
TravelWebDataGrid.Rows(i).Items(11).Value = True
End If
Next
i += 1
Next
Just for testing, only the top Travel Request has documents attached. As you can see, it is checking the second row checkbox as well even though it doesn't have any documents attached.
This is what is being compared to row 2:
"DocName" - E:\DomainWebs\Intranet\fileups\TravelDocs\RONNIEP20180124141116-Hello.docx
"TravelID" - RONNIEP20180124135357
Does anyone know why it is doing this? Let me know if you need any more information. Thanks in advance for your responses.
This might be off because I haven't worked with a datagrid but it looks like the code has been over-complicated with the counter. Can you do this?
' you're looping gridrecords/rows.
For Each GridRecord As GridRecord In TravelWebDataGrid.Rows
For Each doc As String In dirs
' use the gridrecord here, not rows(i).
If doc.Contains(GridRecord.Items(10).Value) = True Then
' and here.
GridRecord.Items(11).Value = True
End If
Next
Next

Mechanize Traverse & Parse

I'm trying to walk through a hitlist of pages found by Mechanize. Upon a search, which is working just fine, I get a hitlist with 10 records per page. A bottom navigation system takes me to the record count > 10. Display 10 per page. So 53 records = 6 "group pages" as I'm calling them.
What I want to do is use the top search results page to do the following:
Grab the html behind every record on this list. I can do that through an iteration.
Follow through the '[Next]' link at the bottom and repeat both 1 and 2 until there is no more 2. Essentially this will get every record.
I'm having an issue with moving off the first page into the second page. I'm grabbing the html behind the first 10 records, but then the system bonks on me. Not sure why. I thought I was iterating through the group pages, but it isn't advancing past the first group page.
counter = 1
puts "Counter: #{counter}"
while agent.page.links_with(:text => '[Next]').count == 1
page = agent.page.link_with(:text => '[Next]').click
puts "Counter/Next: #{counter} / #{agent.page.links_with(:text => '[Next]').count}"
agent.page.links_with(text: '[complete profile]').each do |link|
a = link.click # link.click goes to each company page
r = a.body.to_s
r = r.gsub(/^\s+\n/, "")
# enter company into db
class Company < ActiveRecord::Base
end
company = Company.new
company.cdate = DateTime.now
company.status = 'new'
company.requestID = req_id
company.html = r
company.save
end
counter += 1
end
Any insight appreciated. I know I'm close.
Cheers
Solution: move the click method behind page:
page = agent.click(page.link_with(:text => "[Next]"))

ASP VBScript Server side programming

I am new to the VBScript programming. And just now started programming using ASP. The problem is I have quite 60 options as buttons and have to display the page accordingly. Previously we had 60 functions of same functionality. Now I want to convert it to a single function with select case statement. So I passed different numbers as arguments to the function to get which page to display. But I am getting Overflow when I check the argument value. Is it because type conversion error? I have 2 hierachies. SO am passing 2 digit number like 11. Use 11/10 as first and 11 mod 10 as second level selector
I have some Image and Onclicking the code is the following.
<html>
<head>
<script>
function CallFun(a)
select case int(a/10)
case 1
do something
end select
end Function
</script></head><body><img src="Source.gif" onclick = CallFun(11)></body>
Something like this should work:
function test(param)
dim lvl1 : lvl1 = param\10
dim lvl2 : lvl2 = param MOD 10
Select Case lvl1
Case 1
if lvl2 = 1 then
'some code
end if
Case 2
' some code
Case 3
' some code
Case Else
' some code
End Select
End function
Call test(23) 'for example

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.

Why is this looping infinitely?

So I just got my site kicked off the server today and I think this function is the culprit. Can anyone tell me what the problem is? I can't seem to figure it out:
Public Function CleanText(ByVal str As String) As String
'removes HTML tags and other characters that title tags and descriptions don't like
If Not String.IsNullOrEmpty(str) Then
'mini db of extended tags to get rid of
Dim indexChars() As String = {"<a", "<img", "<input type=""hidden"" name=""tax""", "<input type=""hidden"" name=""handling""", "<span", "<p", "<ul", "<div", "<embed", "<object", "<param"}
For i As Integer = 0 To indexChars.GetUpperBound(0) 'loop through indexchars array
Dim indexOfInput As Integer = 0
Do 'get rid of links
indexOfInput = str.IndexOf(indexChars(i)) 'find instance of indexChar
If indexOfInput <> -1 Then
Dim indexNextLeftBracket As Integer = str.IndexOf("<", indexOfInput) + 1
Dim indexRightBracket As Integer = str.IndexOf(">", indexOfInput) + 1
'check to make sure a right bracket hasn't been left off a tag
If indexNextLeftBracket > indexRightBracket Then 'normal case
str = str.Remove(indexOfInput, indexRightBracket - indexOfInput)
Else
'add the right bracket right before the next left bracket, just remove everything
'in the bad tag
str = str.Insert(indexNextLeftBracket - 1, ">")
indexRightBracket = str.IndexOf(">", indexOfInput) + 1
str = str.Remove(indexOfInput, indexRightBracket - indexOfInput)
End If
End If
Loop Until indexOfInput = -1
Next
End If
Return str
End Function
Wouldn't something like this be simpler? (OK, I know it's not identical to posted code):
public string StripHTMLTags(string text)
{
return Regex.Replace(text, #"<(.|\n)*?>", string.Empty);
}
(Conversion to VB.NET should be trivial!)
Note: if you are running this often, there are two performance improvements you can make to the Regex.
One is to use a pre-compiled expression which requires re-writing slightly.
The second is to use a non-capturing form of the regular expression; .NET regular expressions implement the (?:) syntax, which allows for grouping to be done without incurring the performance penalty of captured text being remembered as a backreference. Using this syntax, the above regular expression could be changed to:
#"<(?:.|\n)*?>"
This line is also wrong:
Dim indexNextLeftBracket As Integer = str.IndexOf("<", indexOfInput) + 1
It's guaranteed to always set indexNextLeftBracket equal to indexOfInput, because at this point the character at the position referred to by indexOfInput is already always a '<'. Do this instead:
Dim indexNextLeftBracket As Integer = str.IndexOf("<", indexOfInput+1) + 1
And also add a clause to the if statement to make sure your string is long enough for that expression.
Finally, as others have said this code will be a beast to maintain, if you can get it working at all. Best to look for another solution, like a regex or even just replacing all '<' with <.
In addition to other good answers, you might read up a little on loop invariants a little bit. The pulling out and putting back stuff to the string you check to terminate your loop should set off all manner of alarm bells. :)
Just a guess, but is this like the culprit?
indexOfInput = str.IndexOf(indexChars(i)) 'find instance of indexChar
Per the Microsoft docs, Return Value -
The index position of value if that string is found, or -1 if it is not. If value is Empty, the return value is 0.
So perhaps indexOfInput is being set to 0?
What happens if your code tries to clean the string <a?
As I read it, it finds the indexChar at position 0, but then indexNextLeftBracket and indexRightBracket both equal 0, you fall into the else condition, and then you insert a ">" at position -1, which will presumably insert at the beginning, giving you the string ><a. The new indexRightBracket then becomes 0, so you delete from position 0 for 0 characters, leaving you with ><a. Then the code finds the <a in the code again, and you're off to the races with an infinite memory-consuming loop.
Even if I'm wrong, you need to get yourself some unit tests to reassure yourself that these edge cases work properly. That should also help you find the actual looping code if I'm off-base.
Generally speaking though, even if you fix this particular bug, it's never going to be very robust. Parsing HTML is hard, and HTML blacklists are always going to have holes. For instance, if I really want to get a <input type="hidden" name="tax" tag in, I'll just write it as <input name="tax" type="hidden" and your code will ignore it. Your better bet is to get an actual HTML parser involved, and to only allow the (very small) subset of tags that you actually want. Or even better, use some other form of markup, and strip all HTML tags (again using a real HTML parser of some description).
I'd have to run it through a real compiler but the mindpiler tells me that the str = str.Remove(indexOfInput, indexRightBracket - indexOfInput) line is re-generating an invalid tag such that when you loop through again it finds the same mistake "fixes" it, tries again, finds the mistake "fixes" it, etc.
FWIW heres a snippet of code that removes unwanted HTML tags from a string (It's in C# but the concept translates)
public static string RemoveTags( string html, params string[] allowList )
{
if( html == null ) return null;
Regex regex = new Regex( #"(?<Tag><(?<TagName>[a-z/]+)\S*?[^<]*?>)",
RegexOptions.Compiled |
RegexOptions.IgnoreCase |
RegexOptions.Multiline );
return regex.Replace(
html,
new MatchEvaluator(
new TagMatchEvaluator( allowList ).Replace ) );
}
MatchEvaluator class
private class TagMatchEvaluator
{
private readonly ArrayList _allowed = null;
public TagMatchEvaluator( string[] allowList )
{
_allowed = new ArrayList( allowList );
}
public string Replace( Match match )
{
if( _allowed.Contains( match.Groups[ "TagName" ].Value ) )
return match.Value;
return "";
}
}
That doesn't seem to work for a simplistic <a<a<a case, or even <a>Test</a>. Did you test this at all?
Personally, I hate string parsing like this - so I'm not going to even try figuring out where your error is. It'd require a debugger, and more headache than I'm willing to put in.

Resources