How to get a return value from a stored procedure using .net 2.0
Suppose you are working with a stored procedure that insert a specific record that has identity on. You want then to return that identity number to insert in another table:
Here is the stored procedure code:
CREATE PROCEDURE dbo.InsertEvent_sp(@EventID INT =0 ,--this is the identity column@EventDate smalldatetime ,
@EventTitle nvarchar(100))AS
BEGIN SET NOCOUNT ON INSERT
INTO tblEvent (EventDate, EventTitle)
values (@EventDate, @EventTitle)SET @EventID = @@IDENTITY
return @EventID
END
GO
Then you would need to use this stored procedure in your SqlDataSource...Then once you insert the record using the datasource, you have to creat a new event for the inserted event: OnInserted="EventDS_Inserted" in the SqlDataSource tag.
Then in the code: EventDS_Inserted would be like:
protected void EventDS_Inserted(object sender, SqlDataSourceStatusEventArgs e){
DbCommand command = e.Command; //do not forget to add using System.Data.Common; in the declarations section.
string eid;
string Speaker, SpeakerTitle;
eid = command.Parameters["@EventID"].Value.ToString();
}
Thursday
How to get a return value from a stored procedure using .net 2.0
Labels:
.Net,
Insert,
SqlDataSource,
Stored Procedures,
Visual Studio 2005,
Web2.0
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment