passing SQL data to ASP

H

Hursh

Hi,

I have written some stored procedures in SQL and these procedures
return some value.
I want these values to be captured by the ASP code.

I am able to access the tables using ADO( recordsets ) but is there a
way to pass data returned from stored procedures to vairables in ASP
code.

tia
Hursh
 
D

Don Grover

Here is a sample against NWind using stored procedures

'''****************************************

<HTML>
<HEAD>
<TITLE>Using Stored Procedures</TITLE>
<%
@LANGUAGE="VBSCRIPT"
%>
<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4"
NAME="ADODB Type Library" -->
<!--
The above line declares the ADO type library so that we can use constants.
For details see
http://msdn.microsoft.com/library/d...wss/_exch2k_working_with_asp_applications.asp
-->
</HEAD>
<BODY>
Calling Parameterised Stored Procedures from ASP
<%

Dim dbUserName
Dim dbPassword
Dim dbCatalog
Dim dbServer

dbServer = "YourServerName."
dbcatalog = "Northwind"
dbUserName = "ia"
dbPassword = "001test"

Dim oConn
Dim strConn
Dim oCmd
Dim rsData
Dim strSQL

strConn="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" &
dbUserName & ";Password=" & dbPassword & ";Initial Catalog=" & dbcatalog &
";Data Source=" & dbServer

Response.Write "Please select a category<BR>"
strSQL = "select CategoryID, CategoryName from categories"

SET oConn = Server.CreateObject("ADODB.Connection")
SET oCmd = Server.CreateObject("ADODB.Command")

oConn.Open strconn
SET rsdata = oconn.Execute (strsql)


%>
<FORM method="post" action="lesson3.asp" ID="Form1">
<select name="iCatID">
<%
' Build the list box
while not rsdata.eof
response.write "<OPTION VALUE = """ +
CSTR(rsdata.fields("CategoryID").value) + """>" +
rsdata.fields("CategoryName").value + "</Option>" + VBCR
rsdata.movenext
wend

%>
</select>
<INPUT type="submit" value="Enter"> <input type="reset" value="reset">
</FORM>
<%
if (Request.Form("iCatID")<>"") then


Set oConn = Server.CreateObject("ADODB.Connection")
Set oCmd = Server.CreateObject("ADODB.Command")


oConn.Open strConn
Set oCmd.ActiveConnection = oConn


' Setup Call to Stored Procedure and append parameters

oCmd.CommandText = "spGetProducts"
ocmd.Commandtype = adCmdStoredProc


oCmd.Parameters.Append ocmd.CreateParameter("@Return_Value", adInteger,
adParamReturnValue)
oCmd.Parameters.Append oCmd.CreateParameter("@CategoryID", adInteger,
adParamInput)



' Assign value to input parameter
oCmd.Parameters("@CategoryID") = Request.Form("iCatID")


' Fire the Stored Proc and assign resulting recordset
' to our previously created object variable

Set rsData = oCmd.Execute
response.Write "Passed in Value = " & Request.Form("iCatID") & "<BR>"
Response.Write "Error Value = " & clng(oCmd("@RETURN_VALUE").Value) &
"<BR>"

%>
<BR>
<BR>
<B>Results</B><BR>
<table width="90%" cellspacing="1" cellpadding="5" border="1">
<%
Dim iCount
'Load the Header Row
response.write "<tr>"
For iCount = 0 to rsData.Fields.Count - 1
response.write "<th>"
response.write rsData.Fields(iCount).Name
response.write "</th>"
Next
response.write "</tr>"

' Load the data Rows
while not rsData.EOF
response.write "<tr>"
For iCount = 0 to rsData.Fields.Count - 1
response.write "<td>"
response.write rsData.Fields(iCount).value
response.write "</td>"
next
response.write "</tr>"
rsData.MoveNext
wend
%>
</table>
<BR>

<FONT color="red">Done.
<br>
</FONT>
<%
end if
%>

</BODY>
</HTML>


''''****************************************
 
B

Bob Barrows

Hursh said:
Hi,

I have written some stored procedures in SQL and these procedures
return some value.
I want these values to be captured by the ASP code.

I am able to access the tables using ADO( recordsets ) but is there a
way to pass data returned from stored procedures to vairables in ASP
code.

tia
Hursh

There are three ways for a stored procedure to return data to a client app:
1. in a resultset - ADO transforms this into a recordset. I assume you are
already familiar with this method, so I will move on
2. in an output parameter. Run this script in QA:
create procedure outparam (@p int output) AS
set nocount on
set @p1 = @p1 + 45
go
declare @retval int
set @retval =2
exec outparam @retval output
select @retval as OutputValue
3. in a Return value. Run this script in QA:
create procedure returnvalue AS
set nocount on
Return 45
go

declare @retval int
exec @retval = returnvalue
select @retval as ReturnedValue

There are many ways to get the data in a resultset using ADO, But there is
only one way in ADO to get the data from output or return parameters: a
Command object.

Dan already showed you an example of one. I imagine that coding it looks a
little intimidating, and you know what? It is intimidating, and error-prone,
as well. That is why many people including myself have created code
generators for Command object code. Mine is available here if you wish to
try it:
http://www.thrasherwebdesign.com/index.asp?pi=links&hp=links.asp&c=&a=clear

HTH,
Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top