Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all

i have this Stored Procedure :

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[TOP_Returner]
    (@number [bigint],
     @return_top nvarchar(200) output
    )
AS
declare @top_name nvarchar(200)

if @number=1
begin
select  top (1) @top_name=[product_name] From View_Product_4_TopBest
set @return_top=@top_name
return @return_top
end

if @number=2
begin
select top (1) @top_name=[color_name] From View_Color_2_TopBest
set @return_top=@top_name
return @return_top
end

if @number=3
begin
select top (1) @top_name=[model_name] From View_Model_2_TopBest
set @return_top=@top_name
return @return_top
end

if @number=4
begin
select top (1) @top_name=[size_name] From View_Size_2_TopBest
set @return_top=@top_name
return @return_top
end

if @number=5
begin
select top (1) @top_name=[group_name] from View_ProductGroup_TopBest
set @return_top=@top_name
return @return_top
end



How Do I use From @return_top In C# Application


Please Help Me


Thank A Lot
Posted

C#
SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter parm=new SqlParameter("@pkid",SqlDbType.Int);
parm.Value=1;
parm.Direction =ParameterDirection.Input ; 
cmd.Parameters.Add(parm); 
SqlParameter parm2=new SqlParameter("@ProductName",SqlDbType.VarChar); 
parm2.Size=50; 
parm2.Direction=ParameterDirection.Output; // This is important!
cmd.Parameters.Add(parm2); 
cn.Open(); 
cmd.ExecuteNonQuery();
cn.Close(); 

// Print the output value
Console.WriteLine(cmd.Parameters["@ProductName"].Value); 
Console.ReadLine();
 
Share this answer
 
Since the @return_top is an output parameter for the procedure, you should set it's value inside the procedure. You don't actually 'return' it, only set the value

When you call the procedure, you define the parameter, you set the parameter.Direction to output. See SqlParameter.Direction Property [^] and ParameterDirection Enumeration[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900