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

# stored procedure with return value

Status
Not open for further replies.

jimsurf

Programmer
Aug 22, 2002
1
0
0
US
I am currently trying to figure out how to use a stored procedure that will return the result of a count query, and I am having some trouble.
Here is the stored procedure:

CREATE PROCEDURE qryVerifyUser
@user_mail varchar(15),
@user_password varchar(15),
@user_count int output
AS
SELECT @user_count =( SELECT Count(*) from new_user
where user_mail=@user_mail
and user_password=@user_password)
GO

The object is to pass the login info from a form and return the count as an int. The code for the aspx page looks like this:

<script language=&quot;c#&quot; runat=&quot;server&quot;>
void Page_Load(){
if (Page.IsPostBack){
if (Request.Form[&quot;Email.Text&quot;] != &quot;&quot;){
SqlConnection aConnection = new SqlConnection
(
System.Configuration.ConfigurationSettings.AppSettings.Get(&quot;SurfJob&quot;)
);
SqlCommand aCommand = new SqlCommand();
aCommand.CommandText = &quot;dbo.[qryVerifyUser]&quot;;
aCommand.CommandType = System.Data.CommandType.StoredProcedure;
aCommand.Parameters.Add(new SqlParameter(&quot;@user_mail&quot;, Request.Form[&quot;Email&quot;]));
aCommand.Parameters.Add(new SqlParameter(&quot;@user_password&quot;, Request.Form[&quot;Password&quot;]));
aCommand.Parameters.Add(new SqlParameter(&quot;@user_count&quot;, 1));

aConnection.Open();
aCommand.Connection = aConnection;
aCommand.ExecuteReader();
Response.Write(&quot;__&quot; + aCommand.Parameters[&quot;@user_count&quot;].Value);
aConnection.Close();


The last add new parameter is the @user_count, but it doesn't return the value from the query. It only retruns the initial value. If I remove the initial value, the code fails. I have seen a bunch of code that sets up a parameter as a return value, but none of the examples I have tried work.

please help.

Jim
 
Maybe you need to set the direction of your parameters before you execute the sproc. In this Windows Form example, Form1 contains button1, textBox1, sqlConnection1, and sqlCommand1. The sproc used in this example is really basic:
Code:
create procedure dbo.nw_CountProductsInACategory(
        @CategoryID int,
        @CountProducts int output
)
AS
SET @CountProducts = (SELECT Count(ProductID) As CountProducts
FROM Products
WHERE CategoryID = @CategoryID)

GO
and it uses the Northwind database running on a local SQL Server/MSDE. The button1_Click event (successfully) passes the value of the output parameter @CountProducts to textBox1.Text.
Code:
private void button1_Click(object sender, System.EventArgs e)
{
    sqlConnection1.ConnectionString = @&quot;server=(local);Trusted_Connection=yes;database=northwind&quot;;
    sqlConnection1.Open();
    sqlCommand1.Connection = sqlConnection1;
    sqlCommand1.CommandType = System.Data.CommandType.StoredProcedure;
    sqlCommand1.CommandText = &quot;nw_CountProductsInACategory&quot;;
    sqlCommand1.Parameters.Add (&quot;@CategoryID&quot;, SqlDbType.Int);
    sqlCommand1.Parameters[&quot;@CategoryID&quot;].Direction = ParameterDirection.Input;
    sqlCommand1.Parameters[&quot;@CategoryID&quot;].Value = 1;
    sqlCommand1.Parameters.Add (&quot;@CountProducts&quot;, SqlDbType.Int);
    sqlCommand1.Parameters[&quot;@CountProducts&quot;].Direction = ParameterDirection.Output;
    sqlCommand1.Parameters[&quot;@CountProducts&quot;].Value = null;
    sqlCommand1.ExecuteScalar();
    this.textBox1.Text = sqlCommand1.Parameters[&quot;@CountProducts&quot;].Value.ToString();
    sqlConnection1.Close();
}
Carter Wickstrom
wickerman26(at)hotmail(dot)com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top