473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

passing SQL data to ASP

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
Jul 19 '05 #1
3 2307
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/de...plications.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>
''''****************************************

"Hursh" <ag***********@yahoo.com> wrote in message
news:c5**************************@posting.google.c om...
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

Jul 19 '05 #2
Hursh wrote:
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/ind...asp&c=&a=clear

HTH,
Bob Barrows



--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #3
Thanx for the help.

I will definitely try the code on your site.

regards
Hursh

"Bob Barrows" <re******@NOyahoo.SPAMcom> wrote in message news:<eN**************@TK2MSFTNGP10.phx.gbl>...
Hursh wrote:
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/ind...asp&c=&a=clear

HTH,
Bob Barrows

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
3
by: Marc Castrechini | last post by:
First off this is a great reference for passing data between the Data Access and Business Layers:...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
3
by: iu2 | last post by:
Hi all, I need your professional opinion about this. It is more a general programming dilemma rather then a C++ one, but since the project I write is in C++... We handle big structs of data....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.