Forum Moderators: open

Message Too Old, No Replies

Wierd bug filling dictionary from SQL

Duplicate Key?

         

aspdaddy

6:33 am on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This one is driving me nuts! I have given up for now and am using an array instead, but if anyone can spot the problem id appreciate it.

The error is always on the third go round the loop when putting the third key in the dictionary. It doesnt matter which table I select from - whether I order ASC or DESC, DISTINCT or not.

The error says I am trying to insert a duplicate key, but this isnt possible as ID in the table is unique constraint and even SELECT DISTINCT gives the same error.

ID in the tables is datatype tinyint,smallint or int and may be non-consecutive numbers, but this doesnt seem to make a difference.

set oDict = server.createobject("Scripting.Dictionary")

fill oDict,"SELECT ID,SomeValue from SomeTable"

sub fill ( d,sql)
dim objConn, objRS, strSQL
set objConn=Server.CreateObject("ADODB.Connection")
openDB( objConn )
set objRS = objConn.Execute ( strSQL )

while not objRS.EOF
d.Add objRS(0), objRS(1)
objRS.MoveNext
wend

closeRS(objRS)
closeDB(objConn)
end sub

I puts in two values and fails on the third, every time.

Now if I change this line:
d.Add objRS(0), objRS(1)
to:
d.Add cstr(objRS(0)), objRS(1)

Everything works fine in the sub, but when come to use the dictonary outside the sub, when I read from it it gives an exception.
:(

emsaw

4:05 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



not sure if you've already tried this, but what does it output to the screen if you do a:

Response.Write objRS(0) & " " & objRS(1) & "<br>" & vbCrLf

does it indeed have all unique values? Possibly a null value that is considered a dup on the second null value?

-Mark

tomasz

6:26 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



I had similar problem in the past and I thin I had to run another Execute instead of Add.

sub fill ( d,sql)
dim objConn, objRS, strSQL
set objConn=Server.CreateObject("ADODB.Connection")
openDB( objConn )
set objRS = objConn.Execute ( strSQL )

while not objRS.EOF
'd.Add objRS(0), objRS(1)
objConn.Execute(Your insert SQL here)
objRS.MoveNext
wend

closeRS(objRS)
closeDB(objConn)
end sub