Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
hi all
i want return one numeric field in sql server and use from it in c#

i wrote this code but not correct

please help me

Stored Procedure Code

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER Procedure [dbo].[Calc_Product_Price]
(@product_id int,
 @Price int output)
as

if not exists (select * from Products where [product_id]=@product_id)
return -1
else
begin
select @Price=[product_sell_price] from Products where [product_id]=@product_id

print @Price
return @Price
end


C# Code :

public int  Calc_Product_price(int Product_id)
{
    SqlConnection MySqlConnection = new SqlConnection(ConnectionMain.strconnection);
    SqlCommand MySqlCommand = MySqlConnection.CreateCommand();
    MySqlCommand.CommandText = "Calc_Product_Price";
    MySqlCommand.CommandType = CommandType.StoredProcedure;

    MySqlCommand.Parameters.Add("@product_id", SqlDbType.BigInt, 8).Value = Product_id;
    MySqlCommand.Parameters.Add("@Price", SqlDbType.BigInt, 8).Value = 0;
    SqlParameter retval = MySqlCommand.Parameters.Add("@Price", SqlDbType.BigInt );
    retval.Direction = ParameterDirection.ReturnValue;


    if (MySqlConnection.State != ConnectionState.Open)
    {
        MySqlConnection.Open();
    }

    MySqlCommand.ExecuteNonQuery();
    int result =(int) MySqlCommand.Parameters["@Price"].Value;

    if (MySqlConnection.State != ConnectionState.Closed)
    {
        MySqlConnection.Close();
    }
    if (result == 1)
    {
        return result;
    }
    else
    {
        return -1;
    }
}
Posted

1 solution

I think your this line should be change like this
C#
retval.Direction = ParameterDirection.ReturnValue;
retval.Direction=ParameterDirection.Output; // This is important!


for more details
http://stackoverflow.com/questions/3433694/how-to-run-the-stored-procedure-that-has-output-parameter-from-c[^]

I hope this will help you.
 
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