Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Stored procedure returns ID
ALTER PROCEDURE [dbo].[InsertValue]
-- Add the parameters for the stored procedure here
@Value1 int,
@Value2 nvarchar(500),
@Id int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
insert into [Values] (Value1, Value2)
values (@Value1, @Value2)
Set @Id = SCOPE_IDENTITY()
END
// Changes to the insert method
public async Task Insert(Value value)
{
using (SqlConnection sql = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand("InsertValue", sql))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@value1", value.Value1));
cmd.Parameters.Add(new SqlParameter("@value2", value.Value2));
var valueIdParameter = cmd.Parameters.Add(new SqlParameter("@Id", value.Value2));
valueIdParameter.Direction = ParameterDirection.Output;
await sql.OpenAsync();
await cmd.ExecuteNonQueryAsync();
var id = int.Parse(valueIdParameter.Value.ToString());
return;
}
}
}
@George-01

This comment has been minimized.

Copy link

George-01 commented Mar 19, 2020

Nice one Felipe. Nicely implemented. What about the call in the controller method to show how this returned Id is handled or passed from repo to the controller's Create Action... Let's say I need to use this Id to implement another action while implementing The Create action in the controller?

Cheers....

@George-01

This comment has been minimized.

Copy link

George-01 commented Mar 28, 2020

Hello Felipe, How do you get this returned 'id' in the calling controller action?

Hope to hear from you soon.

Cheers....

@simplyolumide

This comment has been minimized.

Copy link

simplyolumide commented Apr 22, 2020

how can we create login authentication and log out in this project by protecting some route, and also creating admin route.Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.