ASP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingASP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 30th, 2003, 04:39 PM
hjmf hjmf is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 hjmf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ASP ADO SQL Query

Using ADO connection with DSN I don't have any problems.
Using ADO connection without DSN I have problems using insert and update queries.

An Example (Code is not complete, only revelant parts shown):

m_Provider= "DRIVER=Microsoft Access Driver (*.mdb); DBQ=" & Server.MapPath("\iisadmin") & "\SVV_Db\Links.mdb;"
...
Set m_ObjConn= Server.CreateObject("ADODB.Connection")
m_ObjConn.ConnectionString= m_Provider
m_ObjConn.Open
...
Set rstLinks= Server.CreateObject("ADODB.recordset")
rstLinks.Open m_Query, m_ObjConn, adOpenKeyset, adLockOptimistic
...
with rstLinks
.MoveFirst
.Fields("URL" )= m_URL
.Fields("Name" )= m_SName
.Fields("Description")= m_Description
.Fields("Keywords" )= m_Keywords
.Fields("Misc" )= m_Memo
.Update
End With

works fine.

The following code does not give any error, but does not update anything at all:

m_Provider= "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="& Server.MapPath("\iisadmin") & "\SVV_Db\Links.mdb;"
...
Set m_ObjConn= Server.CreateObject("ADODB.Connection")
m_ObjConn.ConnectionString= m_Provider
m_ObjConn.Open
...
' A Query has been performed returning a valid Id
...
ID= rstLinks.Fields("Id")
Query= "UPDATE [Links] " & _
"SET Name = '" & m_SName & "', " & _
" Description = '" & m_Description & "', " & _
" Keywords = '" & m_Keywords & "', " & _
" Misc = '" & m_Memo & "' " & _
"WHERE [Links].Id=" & ID & ";"
...
Set rstLinks= m_objConn.execute(SQL)
...

This performs without an error but no update has been made.

Does anybody understands why???

Reply With Quote
  #2  
Old July 31st, 2003, 08:40 AM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
Your UPDATE SQL string is being stored in a variable call "Query", whereas you are attempting to run the SQL using a variable called "SQL".

Also, if no update is occuring, are you 100% sure that the ID you are updating exists, as this will not throw an error.

Reply With Quote
  #3  
Old July 31st, 2003, 09:25 AM
hjmf hjmf is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 hjmf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You are right, but that's not the reason why it does not work. As I already stated, this is a shortcut from a large asp file. The function is wrapped in another function where the called parameter is SQL. So I made a slight mistake in copy and pasting this example

Reply With Quote
  #4  
Old July 31st, 2003, 03:33 PM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 6
I would do a Response.Write on your query and make sure it looks the way you want it.

Reply With Quote
  #5  
Old July 31st, 2003, 03:46 PM
hjmf hjmf is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 hjmf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Response.write

I have done a response write and it looks like this:

Record Updated
SQL= UPDATE [Links] SET Name = 'ASP Classes Examples', Description = 'ASP Classes Examples', Keywords = 'asp classes examples vba', Misc = 'ASP Class Modules vba examples.' WHERE [Links].Id=876;

URL = http://www.thevbzone.com/s_asp_classes.htm
Name= ASP Classes Examples
Misc= ASP Class Modules vba examples

In the first part you may notice I have added a dot (.) behind '...vba examples.'
Notice also that behind the response the dot (.) does not appear.

Reply With Quote
  #6  
Old August 1st, 2003, 11:13 AM
aspnewbie aspnewbie is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: The Great White North
Posts: 361 aspnewbie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 50 sec
Reputation Power: 7
Send a message via MSN to aspnewbie
You don't need the semicolon at the end of your update statement. Also you might consider doing a Cint(ID) to ensure that it actually an integer and not a string.

so I would write your statement as follows:

ID= Cint(rstLinks.Fields("Id"))
Query= "UPDATE [Links] " & _
"SET Name = '" & m_SName & "', " & _
" Description = '" & m_Description & "', " & _
" Keywords = '" & m_Keywords & "', " & _
" Misc = '" & m_Memo & "' " & _
"WHERE [Links].Id=" & ID

Another thing you might want to consider is doing a test to see if ID is numeric. If something other than an alphanumberic value is being passed you may want your db to update (to avoid sql injection attacks).

if IsNumeric(ID) then ....

End if

Reply With Quote
  #7  
Old August 1st, 2003, 03:07 PM
hjmf hjmf is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 hjmf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I will show the 2 routines below. The variable m_UseOLEDB is used to switch between both methods.
If m_UseOLEDB is false everything does work fine.
If m_UseOLEDB is true the updates query does not work and gives no error message at all.


' Updates Link information for a given link
Public Function UpdateLink2Database()
Dim rstLinks ' recordset
Dim AUE_Stat ' Add/Update/Edit Status
Dim ID ' ID found

AUE_Stat = cAUE_Unknown
Query = "SELECT * " & _
"FROM [Links] " & _
"Where (Links.URL='" & m_URL & "');"

OpenRecSet rstLinks, Query
if m_UseOLEDB= false then
rstLinks.Open m_Query, m_ObjConn, adOpenKeyset, adLockOptimistic
End if
if rstLinks.EOF then AUE_Stat= cAUE_NotFound else AUE_Stat= cAUE_Found

if AUE_Stat= cAUE_Found then
if m_UseOLEDB= false then
with rstLinks
.MoveFirst
.Fields("URL" )= m_URL
.Fields("Name" )= m_SName
.Fields("Description")= m_Description
.Fields("Keywords" )= m_Keywords
.Fields("Misc" )= m_Memo
.Update
AUE_Stat= cAUE_Updated
End With
else
ID= Cint(rstLinks.Fields("Id"))
Query= "UPDATE [Links] " & _
"SET Name = '" & m_SName & "', " & _
" Description = '" & m_Description & "', " & _
" Keywords = '" & m_Keywords & "', " & _
" Misc = '" & m_Memo & "' " & _
"WHERE [Links].Id=" & ID '& ";"
if OpenRecSet(rstLinks, Query)<> 0 then
AUE_Stat= cAUE_NotFound
else
AUE_Stat= cAUE_Updated
end if
if rstLinks.EOF then
response.write "No records updated"
else
response.write "Record Updated"
response.write "<br>SQL= " & Query
response.write "<br>URL = " & rstLinks.Fields("URL")
response.write "<br>Name= " & rstLinks.Fields("Name")
response.write "<br>Misc= " & rstLinks.Fields("Misc")
Response.write "<br>"
End if
'AUE_Stat= cAUE_Updated
End if
End if

UpdateLink2Database= AUE_Stat

CloseRecSet AUE_Stat
End Function ' UpdateLink2Database


Private Function OpenRecSet(rstR, SQL)
' Init
OpenRecSet= 0

' Body
if not IsObject(rstR) then
if m_UseOLEDB= false then
Set rstR= Server.CreateObject("ADODB.recordset")
else
'response.write SQL
on error resume next
Set rstR= m_objConn.execute(SQL)
if Err.Number> 0 then
OpenRecSet= -1
Response.write "Error: " & Err.Number & "<br>" & Err.Description & "<br>"
End if
on error goto 0
End if
End if
'if m_UseOLEDB= false then response.write SQL & "<br>"
on error goto 0
End Function ' OpenRecSet

Reply With Quote
  #8  
Old August 1st, 2003, 03:39 PM
hjmf hjmf is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 hjmf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

I have found the error. It is rather sneaky.

The update is never reached, because rstR is an object after the select query. The only times this is true, is before the insert and update queries.

Thx all for your help.

The openrecset function has to be as follows:
[pre]
Private Function OpenRecSet(rstR, SQL)

' Init
OpenRecSet= 0

' Body
if not IsObject(rstR) then
if m_UseOLEDB= false then
Set rstR= Server.CreateObject("ADODB.recordset")
End if
End if

if m_UseOLEDB= true then
'on error resume next
Set rstR= m_objConn.execute(SQL)
if Err.Number> 0 then
OpenRecSet= -1
Response.write "Error: " & Err.Number & "<br>" & Err.Description & "<br>"
End if
on error goto 0
End if
'if m_UseOLEDB= false then response.write SQL & "<br>"
on error goto 0
End Function ' OpenRecSet
[/pre]

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingASP Development > ASP ADO SQL Query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway