Conversion failed when Converting the varchar value '@userid' to data type int

Asked

Viewed 804 times

1

The error is what is in the title, converting char to int. " Conversion failed when Converting the varchar value '@userid' to data type int." I appreciate any help and if you have any good practice tips, that’s even better, because I’m new at 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 of 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();
}
  • 1

    Translate the question, here is the pt.stackoverflow ;)

  • 1

    I had not noticed that this section was Portuguese. My mistake thank you :D

  • See if in your Class Recipe the property .UserID is the type string... If it is just modifies to int - and tries to use the Convert.ToInt32() when feeding the property, because if it is a string, probably being fed by a string.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.