VBScript Dictionary Error - dictionary

I'm stuck working on an old project created with classic ASP (VBScript).
I've found the dictionary collection type which seems to be what I need. However, it keeps telling me "This key is already associated with an element of this collection." when I try to load it from a database recordset.
Here is the code.
set objRS = objDBConn.Execute("SELECT AreaAbbrev, Area FROM tblArea WHERE CompanyID = 15")
Set areaDict = CreateObject("scripting.dictionary")
areaDict.CompareMode=vbTextCompare
WHILE NOT objRS.EOF
Response.Write(objRS("AreaAbbrev") & " ::: " & objRS("Area") & "<br />")
areaDict.add objRS("AreaAbbrev"), objRS("Area")
objRS.movenext
WEND
This is the output:
00K ::: Kellogg
00G ::: Girdler
Microsoft VBScript runtime error '800a01c9'
This key is already associated with an element of this collection
As you can see, "00G" is not the same as "00K". Can anyone tell me why this may be giving me this error?
Thanks!

After some further research, I found adding .Value in this line fixed the issue:
areaDict.add objRS("AreaAbbrev").Value, objRS("Area").Value

Related

Not getting attribute from a xml file into asp

I have the following xml result from this link - https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov
<eveapi version="2">
<currentTime>2013-01-16 18:57:38</currentTime>
<result>
<rowset name="characters" key="characterID" columns="name,characterID">
<row name="BorisKarlov" characterID="315363291"/>
</rowset>
</result>
<cachedUntil>2013-02-16 18:57:38</cachedUntil>
</eveapi>
and I am trying to extract the characterID into asp. I am using the following code,
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
oXML.LoadXML("https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov")
Set oRoot = oXML.selectSingleNode("//result")
For Each oNode In oRoot.childNodes
response.Write oNode.Attributes.getNamedItem("characterID").Text
Next
Set oXML = Nothing
All i keep getting is the following error:
Microsoft VBScript runtime error '800a01a8'
Object required: 'oRoot'
.............
I can only assume that Set oRoot = oXML.selectSingleNode("//result") is not actually generating any data and therefore throwing up the error in the next line.
Can anyone please shed some light on my problem?
You have a few problems there.
loadXML() is for loading a block of XML as a string, not fetching from a remote server; for that, you need to use load()
when loading from a server, you need to tell it to use the ServerXMLHTTP component, and set async to false so that it waits until loaded before executing the rest of your script.
when I tried loading that XML, I got an encoding error; you will need to resolve that one way or another
when I loaded the XML directly from a string, it wouldn't parse because there is a script element containing non-XML content; that needs to be contained within a CDATA section
your XPath query is to //result, but you actually need it to be //result/rowset
This code should work once you resolve issues 3 and 4 above:
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
oXML.async = False
oXML.setProperty "ServerHTTPRequest", true
oXML.Load("https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov")
If oXML.parseError.errorCode <> 0 Then
Response.Write "<p>XML parse error: " & Server.HTMLEncode(oXML.parseError.reason) & "</p>"
Else
Set oRoot = oXML.selectSingleNode("//result/rowset")
If oRoot Is Nothing Then
response.write "Nothing!"
response.end
End If
For Each oNode In oRoot.childNodes
response.Write oNode.Attributes.getNamedItem("characterID").Text
Next
End If
Set oXML = Nothing
Edit: to get around the problem #3, and oddly also #4 (don't know why!), use this snippet to load the XML instead. For some reason, I think the code above isn't handling the gzip compressed stream correctly, but this code below does.
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
Set xh = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
xh.open "GET", "https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov", False
xh.send
xml = xh.responseText
oXML.LoadXML xml

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.

Accessing VB6 Collection Item from VBScript embedded in HTML

i'm learning by practice. I was given an OCX file which according to who gave it to me was created using VB6 and I have the task of creating a user interface for it to test all the functionality that is described in a poorly written documentation file. On top of that I am not well-versed in VBScript but I've managed to dodge a few bullets while learning.
I have a method which returns a Collection and when I try to access it from VBScript I am only able to query the Count but when I try to do job.Item(i) or job(i) I get an error stating it doesn't have that property or method.
Can someone point me in the right direction to be able to traverse the contents of this collection?
I had to do it from JavaScript but since some things weren't that easy I decided that perhaps VBScript would help me bridge the gaps where JavaScript didn't cut it. I can access all properties from the ActiveXObject from JavaScript, but the methods which return other VB objects are a little more obscure to me. I've tried aJob.Item(iCount), aJob.Items(iCount) and aJob(iCount).
My code is:
For iCount = 1 To aJobs.Count
MsgBox("Num " & iCount)
MsgBox(aJobs.Item(iCount))
Next
Thanks.
People often create specialized and/or strongly typed collection classes in VB6. They don't always do it correctly though, and they sometimes create "partial" collection implementations that have no Item() method (or fail to mark it as the default member of the class). They might even have a similar method or property but name it something entirely different.
It is rarer to return a raw Collection object, but it can be done and if it is you shouldn't have the problems you have indicated from VBScript.
I just created a DLL project named "HallLib" with three classes: Hallway, DoorKnobs, and DoorKnob. The DoorKnobs class is a collection of DoorKnob objects. The Hallway class has a DoorKnobs object that it initializes with a random set of DoorKnob objects with randomly set properties. Hallway.DoorKnobs() returns the DoorKnobs collection object as its result.
It works fine in this script:
Option Explicit
Dim Hallway, DoorKnobs, DoorKnob
Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()
MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)
For Each DoorKnob In DoorKnobs
MsgBox "DoorKnob.Material = " & CStr(DoorKnob.Material) & vbNewLine _
& "DoorKnob.Color = " & CStr(DoorKnob.Color)
Next
Update:
This script produces identical results:
Option Explicit
Dim Hallway, DoorKnobs, KnobIndex
Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()
MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)
For KnobIndex = 1 To DoorKnobs.Count
With DoorKnobs.Item(KnobIndex)
MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
& "DoorKnob.Color = " & CStr(.Color)
End With
Next
As does:
Option Explicit
Dim Hallway, DoorKnobs, KnobIndex
Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()
MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)
For KnobIndex = 1 To DoorKnobs.Count
With DoorKnobs(KnobIndex)
MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
& "DoorKnob.Color = " & CStr(.Color)
End With
Next
So I suspect you'll need to use some type library browser like OLEView to look at your OCX to see what classes and members it actually exposes.

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.

How do I traverse a collection in classic ASP?

I want to be able to do:
For Each thing In things
End For
CLASSIC ASP - NOT .NET!
Something like this?
dim cars(2),x
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars
response.write(x & "<br />")
Next
See www.w3schools.com.
If you want to associate keys and values use a dictionary object instead:
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
if objDictionary.Exists("Name") then
' Do something
else
' Do something else
end if
Whatever your [things] are need to be written outside of VBScript.
In VB6, you can write a Custom Collection class, then you'll need to compile to an ActiveX DLL and register it on your webserver to access it.
The closest you are going to get is using a Dictionary (as mentioned by Pacifika)
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = vbTextCompare 'makes the keys case insensitive'
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
But I loop through my dictionaries like a collection
For Each Entry In objDictionary
Response.write objDictionary(Entry) & "<br />"
Next
You can loop through the entire dictionary this way writing out the values which would look like this:
Scott
20
You can also do this
For Each Entry In objDictionary
Response.write Entry & ": " & objDictionary(Entry) & "<br />"
Next
Which would produce
Name: Scott
Age: 20
One approach I've used before is to use a property of the collection that returns an array, which can be iterated over.
Class MyCollection
Public Property Get Items
Items = ReturnItemsAsAnArray()
End Property
...
End Class
Iterate like:
Set things = New MyCollection
For Each thing in things.Items
...
Next
As Brett said, its better to use a vb component to create collections. Dictionary objects are not very commonly used in ASP unless for specific need based applications.
Be VERY carefully on using VB Script Dictionary Object!
Just discover this "autovivication" thing, native on this object: http://en.wikipedia.org/wiki/Autovivification
So, when you need to compare values, NEVER use a boolen comparison like:
If objDic.Item("varName") <> "" Then...
This will automatically add the key "varName" to the dictionary (if it doesn't exist, with an empty value) , in order to carry on evaluating the boolean expression.
If needed, use instead If objDic.Exists("varName").
Just spend a few days knocking walls, with this Microsoft "feature"...
vbscript-dictionary-object-creating-a-key-which-never-existed-but-present-in-another-object

Resources