ASP VBScript Server side programming - asp-classic

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

Related

How to remove duplicates in a listbox VB.net

I am trying to remove duplicates in a ListBox which is populated by a query pull. I use this code to prevent adding duplicates in VB 6.0 but does not work when converted over to VB.net. Is there a substitute method to prevent or remove duplicates.
colSchema = dr("Col_Schema").ToString
If Not lstSchema.Items.ToString.Contains(colSchema) Then
lstSchema.Items.Add(New ListItem(colSchema))
End If
try
colSchema = dr("Col_Schema").ToString
dim exists as boolean = false
for i as integer = 0 to lstSchema.items.count - 1
if lstSchema.items.item(i) = colSchema then
exists = true
end if
next
if exists = false then
lstSchema.Items.Add(New ListItem(colSchema))
end if
This code
lstSchema.Items.ToString
is converting Items to a string. Items is most likely the type ListBox.ObjectCollection (if this is WinForms) or a similar collection type for other UI frameworks. Calling ToString on such classes will end up calling Object.ToString, which just returns the name of the class.
Instead, try
lstSchema.Items.Contains(colSchema)
If that does not work for some reason, please update your question explaining exactly what you were trying to solve by calling ToString.

delete an element from an array in classic ASP

Given the following array as an example...
arr(0)(0) = 3
arr(0)(1) = name
arr(0)(2) = address
arr(1)(0) = 7
arr(1)(1) = name
arr(1)(2) = address
arr(2)(0) = 14
arr(2)(1) = name
arr(2)(2) = address
I need to delete the middle element (id=7) from the array. I understand that I need to loop through the array and move each record that isnt to be deleted into a new array. I tried like this...
Dim newArr,i
Redim newArr(Ubound(arr))
For i = 0 to Ubound(arr)
If (CStr(arr(i)(0)) <> 7 ) Then
newArr(i) = arr(i)
End if
Next
When debugging this I can see the if statement work so I know only 2 elements are copied but newArr is empty at the end of this. What am I missing. I am a PHP coder that is new to classic asp and Im used to having array functions that make this kind of thing unnecessary. Any help appreciated. Thank you.
You don't need new array, you can just reassign the items and "crop" the array:
Const removalIndex = 1
For x=removalIndex To UBound(arr)-1
arr(x) = arr(x + 1)
Next
ReDim Preserve arr(UBound(arr) - 1)
This code will remove the array item at index 1 from the main array. If you don't know in advance the index of the item to remove, you can easily find it with a simple loop over the array.
Instead of using array you can give Scripting.Dictionary a try.
It is much more flexible, and has, among others Remove method.
I suggest using Scripting.Dictionary and using it as a List/collection instead, as it allows for insertions and deletions. See here: Lists in VBScript
I don't know the definitive answer, but if I were to take a stab in the dark guess I'd suggest that since the array is two dimensional maybe you have to explicitly refer to it that way?
Dim newArr,i
Redim newArr(Ubound(arr),3)
For i = 0 to Ubound(arr)
If (CStr(arr(i)(0)) <> 7 ) Then
newArr(i)(0) = arr(i)(0)
newArr(i)(1) = arr(i)(1)
newArr(i)(2) = arr(i)(2)
End if
Next
I see some VBScript syntax issues. First:
arr(0)(0) = 3 'ERROR: Subscript out of range
arr(0, 0) = 3 'CORRECT
Next:
ReDim newArr(Ubound(arr)) 'this is 1 dimensional array
newArr(0) = arr(0) 'this will NOT work
newArr(0) = arr(0, 0) 'this will work
And finally: why you convert to String and then compare it to an Integer with:
(CStr(arr(i)(0)) <> 7)

If PageCount ist reached, start from First Page ( 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.

How to display an image if the value has a decimal point?

Basically in my in my aspx page I have a gridview which displays the value from my database as an image. So if a value in my database table is 5, it will be displayed as 5 images in the gridview. ie.(star.jpg star.jpg star.jpg star.jpg star.jpg)
The code:
Protected Function getrating(ByVal rate As Integer)
Dim getrating As String
getrating = ""
For i = 1 To rate
getrating = getrating + "<img src=""Images/star.jpg"" alt=""*"">"
Next
Return getrating
End Function
It's been working fine so far for whole numbers, but now I'm adding averages into my database, so any value with a decimal point(like 4.6) gives me the error
"Conversion from type 'DBNull' to type 'Integer' is not valid."
How would I go about in adding images when the value has a decimal point?
Since the field in my database has the range set to numbers 1 to 5, I like it to display another image if the value has a decimal point. ie. "3.5" would display in the gridview star.jpg, star.jpg, star.jpg, halfstar.jpg. If that made any sense lol.
Anyone have an idea on how to do doing this?
Judging by your error, it's probable that you're not even accepting decimal values and inserting the NULL value when they occur. Fix that first, make sure the result isn't DBNull, then you can make the changes to a decimal type like Double:
Protected Function GetRating(ByVal rating As Double) As String
Dim result As New System.Text.StringBuilder()
While rating >= 1.0#
result.Append("<img src=""Images/star.jpg"" alt=""*"">")
rating -= 1.0#
End While
If rating > 0.0# Then _
result.Append("<img src=""Images/halfstar.jpg"" alt=""1/2"">")
Return result.ToString()
End Function
I also took the liberty of using a StringBuilder instead of concatenating strings with +.
You need to check DBNull value (From error description).
If reader.Read() Then
IF Not reader.IsDBNull(0) Then '1st column
'If field type is decimal
Dim decimalVar=reader.GetDecimal(0)
End If
End If
If you want to do this I really recommend to use one image which contains all stars in it. So that image should have 5 Stats like:
For this demo I added a image with 120px width and 24px height. Then You need to add a like this:
Then when you want to show your stars, you need to find out the width of this with following method:
DIV width = (Rate/5) * 120
So for example 2.5 will be (2.5/5)*120 = 60! Then you need to change DIV's width to 60 and then you'll have your 2.5 rank! and will become something like:
<div style="background-image:url(http://s.codeproject.com/script/Ratings/Images/stars-fill.png);width:60px;height:24px;">
</div>
This is just a simple method which will give you an idea to expand and customize for your logic. I hope this helps :-)
Live Demo: http://jsfiddle.net/qxrub/

Vary a variable-name in a for loop

Im looking for help with trying to vary a variable-name in a for loop:
For b = 1 To Count
' the below image_var(b) varible needs to vary on each iteration'
Dim image_var(b) As New LinkedResource(Server.MapPath(myArray(b-1)))
' need also to have the b in myArray(b-1) bit work through the loop too'
' that is I''m after the array index b minus 1'
image_var(b).ContentId = "imageContentId_" + b.ToString
' add the LinkedResource to the appropriate view'
htmlView.LinkedResources.Add(image_var(b))
Next b
Thanks in advance as I can't logon to accept an answer...
Thanks Guffa - my pics are now making it through to the email and showing up.
The (b) in the image_var(b) was just a stub for me too - till I found the code I was after... Am new and didn't even know/realise that it made an array... am a nobb.
Thanks again...
I don't know why you think that you need a separate variable for each instance. The variable just holds a reference to the object, it doesn't matter at all what you call the variable. You can just reuse the same variable for each of the objects:
For b = 1 To Count
Dim image As New LinkedResource(Server.MapPath(myArray(b-1)))
image.ContentId = "imageContentId_" + b.ToString
' add the LinkedResource to the appropriate view'
htmlView.LinkedResources.Add(image)
Next b

Resources