Retrun Value from Procedure using Parameter discovery

Topics: Data Access Application Block
Feb 26, 2010 at 9:11 AM

Hi All,

How i jsut wondering how can we get the OUTPUT value(s) after executing an procedure using parameter discovery  , i have gone thru the document and it was mention you need to pass 'null' for OUTPUT values .

it will be great full if anybody share an example .

Below is my code to execute a proc , which accepts all in values

<font size="2" color="#2b91af"><font size="2" color="#2b91af">

DbCommand 

</font></font><font size="2" color="#2b91af">

 

</font>

cmd = db.GetStoredProcCommand("AddProduct", m_ProductName , m_ProductUOM ,m_ProductSellingPrice);

Feb 26, 2010 at 10:07 AM

Hi,

Here is a sample:

I have a table named Products with columns ProductID, ProductName, Type,Count . Then I have this stored procedure which inserts product then returns the ProductID, see the sproc below.

CREATE PROCEDURE [dbo].[INSERTPRODUCT]
	@ProductID INT OUTPUT,
	@ProductName nvarchar(50),
	@Type nvarchar(50),
	@Count int
AS
BEGIN
	INSERT INTO
	[Products]
	(ProductName, [Type], [Count])
	VALUES
	(@ProductName,
	@Type,
	@Count)
	
	SELECT @ProductID = @@IDENTITY
END

 And here is the sample code:

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand("INSERTPRODUCT", null, "aaaa", "bbb", 3);
            db.ExecuteNonQuery(cmd);
            //This line gets the output value
            int id = (int)db.GetParameterValue(cmd, "@ProductID");

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

  

 

Feb 26, 2010 at 10:13 AM

Hi ,

i am very much impressed with your prompt reply and answer you gave  with very clear example .

Thanks your support .