August 9, 2007 at 3:30 pm
I am struggling to learn the in's and out's of VBScript and could use a push in the right direction.
I have a working config.inc file that establishes connection with my SQL server. When I use the scripting of <% code here %> I have no probems using the Conn.Execute("") statement to interact with SQL.
When my subroutine is run, I am getting the error that the Conn object does not exist. Below is my code so hopefully someone can help. I'm stumped!
<%@ LANGUAGE = "VBSCRIPT" %>
<!--#INCLUDE FILE="config.inc"-->
<SCRIPT LANGUAGE="VBScript">
<!--
Sub btnSave_OnClick
dim a bunch of variables
sql = "the sql statement"
Conn.Execute(sql)
End Sub
//-->
</SCRIPT>
<html>
<head>
I must be overlooking something minor but I can't put my finger on it. Thanks everyone.
August 9, 2007 at 3:51 pm
<!--#INCLUDE FILE="config.inc"-->
<%
Sub btnSave_OnClick
dim a bunch of variables
sql = "the sql statement"
Conn.Execute(sql)
End Sub
%>
<html>
<head>
August 10, 2007 at 7:27 am
Thank you for the quick response but for some reason when I set it up this way, clicking the button does nothing.
I really don't understand the difference between <% %> and Using <Script> </Script>
I was under the impression that <% %> means it is run when the page is loaded and <Script> is used for subroutines?
August 10, 2007 at 8:13 am
August 10, 2007 at 8:44 am
Here is my code - it is a .ASP page. The code comes from my test machine which isn't connected to the network (for testing reasons) so I retyped this code out - if there are typos please ignore. As the code is right now, it compiles correctly but just doesn't do anything upon clicking.
<%@ LANGUAGE = "VBSCRIPT" %>
<!--#INCLUDE FILE="config.inc"-->
<%
dim accountID,getComment,locateCode
accountID = Request.QueryString("id")
getComment = Request.QueryString("comments")
locateCode = Request.QueryString("locate")
%>
<Script Language = "VBScript">
<!--
Sub btnSave_OnClick
dim TheForm,account,comment,revisedComment,fixComment
Set TheForm = Document.forms("CommentForm")
account = TheForm.accountNum
comment = TheForm.skipComments
revisedComment = Replace(comment, "'","''")
fixComment = "the sql statement to execute"
Conn.Execute(fixComment)
//-->
</Script>
<html>
<head>
<meta name="GENERATOR" content="Microsoft Frontpage 5.0">
<meta name="ProgID" content="FrontPage.Editors.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>EditComment</title>
</head>
<body>
<form name="CommentForm">
Status:<br /><input type="text" name = "status" size = "5" READONLY value = "<%response.write locateCode%>"><br />
Account #:<br /><input type = "text" name = "accountNum" size="10" READONLY value="<%response.write accountID%>"><br />
Skip Comments:<br /><textarea rows="3" cols="31" name="skipComments" wordwrap="hard"><%response.write getComment%></textarea><br />
<input type = "Button" name = "btnSave" value="Save Comments">
</form>
</body>
</html>
August 10, 2007 at 9:45 am
My apologies, the reason the code is doing nothing is because I don't have End Sub
Sub btnSave_OnClick
dim TheForm,account,comment,revisedComment,fixComment
Set TheForm = Document.forms("CommentForm")
account = TheForm.accountNum
comment = TheForm.skipComments
revisedComment = Replace(comment, "'","''")
fixComment = "the sql statement to execute"
Conn.Execute(fixComment)
Here
//-->
Now I am getting the error that Conn doesn't exist again.
August 10, 2007 at 10:00 am
August 10, 2007 at 12:01 pm
I'm pretty sure it's defined properly in my include file - I put an OPTION EXPLICIT at the top of the asp page and still am having the same problem. The wierd thing is that the Conn variable is recognized and executes with no problem when using it within a <% %> statement. But when I do that, it doesn't execute the subroutine when clicking my button.
And if I use <Script>, then it doesn't recognize Conn as a variable. Any other ideas?
August 14, 2007 at 8:33 am
<script> tags are for client side usage, whereas stuff inside <% %> would be executed at the server.
the connection does not exist on the clientn side of course.
if your form has a method of "GET", then you use Request.QueryString to get all the values form the submitted form.
if your form has a method of "POST", then you use Request.Form to get all the values form the submitted form.
note how if there is no item named "btnSave" with a specific value, the code will not be executed.
you want something like this:
<%
dim TheForm,account,comment,revisedComment,fixComment
if Request.Form("btnSave") = "Save Comments" then
set cnObj = Server.CreateObject("ADODB.Connection")
cnObj.ConnectionString= "Provider=SQLOLEDB;Server=192.168.1.1;database=GHBA;network=DBMSSOCN;uid=tester;pwd=password"
'cnObj.ConnectionString="Provider=SQLOLEDB.1;Password=welcome;Persist Security Info=False;User ID=tester;Initial Catalog=GHBA;Data Source=WEBDEV"
cnObj.open ' // Use of Network=DBMSSOCN is to avoid Named Pipes errors
account = Request.Form("accountNum")
comment = Request.Form("skipComments")
revisedComment = Replace(comment, "'","''")
fixComment = "the sql statement to execute"
cnObj.Execute(fixComment)
End If
%>
Lowell
August 14, 2007 at 9:55 am
Very interesting - much better it seems to work now so much appreciated. Thanks for the explanation - (a lot easier to comprehend than a code snippet)
So the reason I am setting my code up like this is so I can, edit the textarea, update the database, and then go back to the previous page (needs refreshed) to show that the update has taken place.
I have been trying to imitate code that is already in place but can't seem to find a workaround for it.
So now I have this:
<%
dim TheForm,account,comment,revisedComment,fixComment
if Request.Form("btnSave") = "Save Comments" then
Do the SQL Update Stuff
(Here I need to get back to previous page - http://ourserver/lastpage.asp?where=###&Purge=&Account=#######
End if
%>
Other code snippets show something like this
<Script LANGUAGE="javascript">
<!-- function showReports() {
location.href = "http://ourserver/lastpage.asp?where=###&Purge=&Account=#######"
}
//-->
</SCRIPT>
And then within my <% %> from above I would need to do this:
cnObj.Execute(fixComment)
Call showReports ()
End If
%>
I'm getting a programming error when trying this because I don't think you can call a function like that within the <% %> server side code like that.
Is there a way around this, or better yet, a less awkward way to do it to begin with? This can't be that difficult, but I just need to be able to stick variables into the href statement to get me back to the correct page. Thanks again for the lesson.
August 14, 2007 at 12:07 pm
sent you a PM, as we might be getting sidetracked in ASP code vs SQL for the forum. I hate posting huge volumes of code and examples which might side track us.
hopefully I can provide you with some good suggestions for your ASP page.
Lowell
August 14, 2007 at 12:22 pm
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply