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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No data from a simple ASP db Connection

Status
Not open for further replies.
Sep 27, 2001
179
AU
Hi

I am trying to create a very simple ASP page that takes a value from a form into a variable and then displays the results of an SQL query based on that variable. The result should just be a single record. The problem I am having is that the ASP page produces no results.

For testing purposes I outputted the contents of the variable and SQL query used to ensure these are correct, and they are.

If I changed the SQL query so that it does not use a Where clause, I would expect that the output would be all records in the database. Which is what I get, so this works.

So the variable is correct, the SQL query is correct. But the results are not being produced, I can't see what is wrong.

The database I am connecting to is SQL.

The code I am using is as follows:
Code:
<%@ Language=VBScript %>
<%
Option Explicit
%>
<% 
'Declare variables
Dim rsGoldMine 			'as adodb.recordset
Dim sGoldMineACCOUNTNO		'GoldMine AccountNo	
Dim sGoldmineConnectionString 	'as Connection string
Dim cnnGoldmine 		'as connection
Dim sGoldmineDatabaseName 	'as database string
Dim strSQL 			'String to hold SQL query

'Populate Variables
sGoldmineConnectionString="Provider=MSDataShape;DSN=GoldMine;UID=sa;PWD="
sGoldmineDatabaseName="GMDemo"
sGoldMineACCOUNTNO=Request.Form("ACCOUNTNO")

'Connection to Database
Set cnnGoldmine = Server.CreateObject("ADODB.Connection")
cnnGoldmine.Open sGoldmineConnectionString

set rsGoldMine = Server.CreateObject("ADODB.Recordset")
rsGoldMine.ActiveConnection=cnnGoldmine
'[COLOR=red ]The query should pull in the variable to produce a restricted result, if query is change to simply Select * from contact1 it produces results[/color]
strSQL = "select COMPANY from Contact1 Where Accountno ='" & sGoldMineACCOUNTNO & "'"
rsGoldMine.Open strSQL, cnnGoldMine

'[COLOR=red ] The variables get outputted correctly [/color]
Response.Write (sGoldMineACCOUNTNO) & "<BR>" 'Output the variable populated from form
Response.Write (strSQL)   'Output SQL query string to check
%>
<html>
<head>
</head>
<%

WHILE not rsGoldMine.EOF 
	Response.Write ("<br>")	
	Response.Write (rsGoldMine("COMPANY"))
	Response.Write ("<br>")
	rsGoldMine.MoveNext
WEND

'Reset server variables
cnnGoldmine.Close

Set cnnGoldmine= Nothing
Set rsGoldMine = Nothing

%>
</body>
</html>

Thanks

Rob
 
Try this:

strSQL = "select COMPANY from Contact1 Where Accountno =" & sGoldMineACCOUNTNO


-VJ

 
Hi

Thanks for the response but this didn't work I get the following error

Code:
Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'A3050650133'.

The Accountno field is text, in this case the field the AccountNo is A3050650133*KUGV8And

If I change the query to the following to include the Accountno it works.

Code:
strSQL = "select COMPANY from Contact1 Where Accountno ='A3050650133*KUGV8And'"

Rob
 
Sorry i thought accountno to be a number and not to be a text.

Anyways what does your Response.Write strSQL look like.

Thanks
VJ
 
You could try using the Trim function to ensure that no spaces are sent at the start or end of the account number
Code:
sGoldMineACCOUNTNO = Trim(Request.Form("ACCOUNTNO"))

Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
Hi

Added the trim() function but still no joy :-(

The output from the two Response.Write statements is:

Code:
A2112651181$]KA[8Hel 
select COMPANY from Contact1 Where Accountno ='A2112651181$]KA[8Hel '

If I cut and paste the outputted SQL query into Query Analyser it works.

Rob
 
How come there is still a space at the end of the account number in the SQL string? Is that after the Trim() function has been applied to the Request.Form line?


Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
try this:

If LEN(TRIM(Request.Form("ACCOUNTNO"))) > 0 Then
sGoldMineACCOUNTNO = Request.Form("ACCOUNTNO")
End If

Then execute the query

strSQL = "select COMPANY from Contact1 Where Accountno ='" & sGoldMineACCOUNTNO & "'"

hth

-VJ

 
Hi

Yeah it is, I notice that, although the query does run in query analyser

The AccountNo is being read in from a form that is automatically submitted on loading. The field is being pre-populated with the accountno from an application.

I just changed the form so that I have to manually type in the value then click a submit button. This is now working, so it looks like a problem with the form.

Rob

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top