Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Created March 18, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavilanch/00380ffc4ac5981ea8e0ac53a57a84db to your computer and use it in GitHub Desktop.
Save gavilanch/00380ffc4ac5981ea8e0ac53a57a84db to your computer and use it in GitHub Desktop.
// 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
Copy link

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
Copy link

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

Hope to hear from you soon.

Cheers....

@simplyolumide
Copy link

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