我在SQL Server中的存儲過程返回一個值:使用SQL數據源,其配置像SQL數據源 - 存儲過程和返回參數
CREATE PROCEDURE [dbo].[insertProc]
@value1 INT,
@value2 INT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO table1(value1,value2) VALUES (@value1,@value2)
RETURN SCOPE_IDENTITY();
END
我連接到數據庫從ASP.NET這個:
InsertCommand="@insertedId = insertProc"
InsertCommandType="StoredProcedure"
oninserting="sqlDS_Inserting"
oninserted="sqlDS_Inserted"
<InsertParameters>
<asp:Parameter Name="value1" />
<asp:Parameter Name="value2" />
<asp:Parameter Name="insertedId" DbType="Int32" Direction="ReturnValue" />
</InsertParameters>
我想做什麼來得到返回的值。在sqlDS_Inserted
程序的身體我這樣做:
this.insertedId = Convert.ToInt32(e.Command.Parameters["insertedId"].Value);
,但我得到的錯誤:@insertedId變量
Object cannot be cast from DBNull to other types.
但是,當我看着SQL Server設置和運行命令(添加的聲明)它運作良好。什麼是問題,以及如何從ASP.NET獲取存儲過程的返回值?
不幸的是您是對的!我開始時嘗試使用和不使用@insertedId(以及許多其他選項),在OnInserted中使用或不使用@,但我認爲我從未嘗試過這種配置。不管怎樣,謝謝。它現在有效。 – 2009-11-05 14:44:49