The error is what is in the title, conversion char to int. "Conversion failed when converting the varchar value '@userID' to data type int." Thanks for any help and if you have any tips on good practice, it's even better, because I'm new to this and I'm learning on my own. Thank you for your attention.
CREATE PROCEDURE Save_Recipe
(@userID int,
@recipeName nvarchar(50),
@recipeDuration nvarchar(50),
@recipeDificultyID int,
@recipePreparation nvarchar(MAX))
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [Taverna_Da_Esquina].[dbo].[Recipe] ([userID],[recipeName],[recipeDuration],[recipeDificultyID],[recipePreparation])
OUTPUT inserted.[recipeID]
VALUES (@userID,@recipeName,@recipeDuration,@recipeDificultyID,@recipePreparation)
END
EXECUTE Save_Recipe 1,nome,duracao,1,preparacao
Results in error:
Conversion failed when converting the varchar value '@userID' to data type int.
This is the C # code for my application:
public void Save(Recipe recipe)
{
conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("Save_Recipe",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@userID", recipe.UserID));
cmd.Parameters.Add(new SqlParameter("@recipeName", recipe.RecipeName));
cmd.Parameters.Add(new SqlParameter("@recipeDuration",recipe.RecipeDuration));
cmd.Parameters.Add(new SqlParameter("@recipeDificultyID", recipe.RecipeDificultyID));
cmd.Parameters.Add(new SqlParameter("@recipePreparation", recipe.RecipePreparation));
try
{
conn.Open();
int receitaID = (int)cmd.ExecuteScalar();
//Associar os ingredientes à receita
SqlCommand cmd2 = new SqlCommand("Recipe_AddIngredientList",conn);
cmd2.Parameters.Add("@recipeID", System.Data.SqlDbType.Int);
cmd2.Parameters.Add("@ingredientID", System.Data.SqlDbType.Int);
cmd2.Parameters.Add("@quantity", System.Data.SqlDbType.Float);
cmd2.Parameters.Add("@measureID", System.Data.SqlDbType.Int);
foreach (Ingredient i in recipe.RecipeListIngredientes)
{
cmd2.Parameters["@recipe_id"].Value = receitaID;
cmd2.Parameters["@ingredient_id"].Value = i.IngredientID;
cmd2.Parameters["@quantity"].Value = i.IngredientQuantity;
cmd2.Parameters["@medida"].Value = i.IngredientMeasureID;
cmd2.ExecuteNonQuery();
}