Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confusing "Object Required" Error...

Status
Not open for further replies.

OzWolf

Programmer
May 22, 2002
52
AU
Greetings all,

I have run into a confusing issue with an ASP page that is trying to execute an SQL statement on a MySQL database.

Here is the code first:
Code:
<!-- #include file="../functions.asp" -->
<!-- #include file="../retrieve_db.asp" -->
<%
	on error resume next
	Dim strSQL
	Dim intParentID
	Dim strDisplay
	Dim strMenuType
	Dim strURL
	Dim intDisplayOrder
	Dim recTest
	
	Set recTest = Server.CreateObject("ADODB.Recordset")

	intParentID = Request.Form("parent_id")
	strDisplay = Request.Form("display")
	strMenuType = Request.Form("menu_type")
	strURL = Request.Form("url")
	intDisplayOrder = Request.Form("display_order")
	
	strDisplay = Replace(strDisplay, "'","''")
	
	Response.Write("<b>SQL Statement</b><br>")
	
	strSQL = "INSERT INTO menu (display, menu_type, url, display_order, parent_id) " & _
			 "VALUES ('" & strDisplay & "', " & _
			 "'" & strMenuType & "', " & _
			 "'" & strURL & "', " & _
			 intDisplayOrder & ", " & _
			 intParentID & ");"
	
	Response.Write(strSQL & "<br>")
	
	Response.Write("<br><b>Return Connection String</b><br>")
	
	Response.Write(dbConn.ConnectionString & "<br>")

	if err.number <> 0 Then
		Response.Write("1: " & err.number & " - " & err.description & "<br>")
	End If
	
	err.number = 0
	
	Response.Write("<br><b>Execute SQL</b><br>")
	
	dbConn.Execute strSQL
	
	if err.number <> 0 Then
		Response.Write("2: " & err.number & " - " & err.description & "<br>")
	End If
	
	err.number = 0
	
	Response.Write("<br><b>Folder Structure</b><br>")
	
	strSQL = "SELECT * FROM menu"
	
	With recTest
		.Open strSQL, dbConn
		If Not .EOF Then
			.MoveFirst
			While Not .EOF
				Response.Write(.Fields("display").Value & "<br>")
				.MoveNext
			Wend
		End If
		.Close
	End With
	
	if err.number <> 0 Then
		Response.Write("2: " & err.number & " - " & err.description & "<br>")
	End If
	
'	Response.Redirect("manage_menu.asp")
%>

Basically, the database connector (dbConn) is retrieved via the included retrieve_db.asp file. I have put some formatting in here to try and track the problem. All this page is supposed to do is insert a record into a table called menu then return the user to the management screen.

However, anytime the database connector is directly referenced, the system spits out an "object required" error. What makes it confusing is that using the same object to open a recordset is no problem at all.

Here is a printout of the results:

Code:
SQL Statement
INSERT INTO menu (display, menu_type, url, display_order, parent_id) VALUES ('Navy', 'm', 'XX', 1, 1);

Return Connection String
1: 424 - Object required

Execute SQL
2: 424 - Object required

Folder Structure
Admin
Menu

Any help on this issue would be greatly appreciated. I have used this way of doing things in the past without a problem connecting to a remote Oracle database so I can't figure what is wrong. Even declaring a new database connector within this code fails to do anything. I've even tried using the database admin account to login (which has no problems using the remote admin tool).
 
if you use <%option explicit %> (as you always should) You will find out if the object is getting defined. and remove the "on error" handler until you have debugged, it will be stopping you seeing the errors. Use Tools -> Internet Options -> Advanced -> uncheck "Show friendly HTTP error messages"

I would suspect the use of parent paths (../) for your include file statement. Only when the included files are in the folder above the file that is executing will the variables (and objects) be defined. Use a root relative path for your includes.


Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
I don't see the declaration or instantiation (CreateObject) of the Connection object...
I guess it is possible your doing it in one of your include files, but their filenames seem to suggest that if that were the case you would have to call a function to do it, at the very least. If the variable is not defined outside of the function than it will not be available after the function exits because the scope will be for that function, not global to the whole page.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Need an expensive ASP developer in the North Carolina area? Feel free to let me know.


 
Ah, fixed it.

The problem was over declaring things. The retrieve_db.asp page is a simple-to-call slice of code that declares an ADO connection and connects it to the database using a function called Connect in functions.asp.

Within retrieve_db.asp, I was declaring dbConn, instantiating it, then instantiating it again. Example:

Code:
Dim dbConn

Set dbConn = Server.CreateObject("ADODB.Connection")

Set dbConn = Connect("username","password")

I simply removed the Set dbConn = Server... line and it all works properly now. Nothing like being over specific to stuff it all up...hehe.
 
A prime example of why you should use <%option explicit%> when coding. Errors like that will show up as soon as the page is run and are reported as the correct error, instead of the code exhibiting a strange logic error or throwing some other seemingly unrelated error.



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top