Search code examples
c#databaseinsertdapper

'Must declare the scalar variable "@categoryid". When im trying insert data from procedure to database with dapper


I'm trying to insert data to my database with a simple procedure but when I click my button I keep getting back:

'Must declare the scalar variable "@categoryid".

I can add that another data addition without a variable works without a problem.

Where do I make a mistake?

Insert class:

public partial class B_Line
{
    public B_Line()
    {
        this.Budget_Scheme = new HashSet<Budget_Scheme>();
    }

    public int Line_id { get; set; }
    public int Category_id { get; set; }
    public string Name { get; set; }
    public string Comment { get; set; }

    public void InsertLine(int categoryID, string name, string comment)
    {
        using (var connection = Connection.ConnectionFactory.GetOpenConnection())
        {
            List<B_Line> lines = new List<B_Line>();
            lines.Add(new B_Line {Category_id = categoryID, Name = name, Comment = comment});
            connection.Execute("dbo.Insert_Line @categoryid, @name, @comment", lines);
        }
    }
}

Excecute button:

private void Button1_Click(object sender, EventArgs e)
{
    B_Line lines = new B_Line();
    int ID_Kategorii = Int32.Parse(categoryID.Text);
    lines.InsertLine(ID_Kategorii, nameLine.Text, commentLine.Text);
}

Stored procedure MSSQL:

ALTER procedure [dbo].[Insert_Lines](@categoryid int, @name nchar(40), @comment nchar(100)) as insert into BCategory (Category_id, Name, Comment) Values (@categoryid, @name, @comment)

Solution

  • You will find all the help you need here:

    https://medium.com/dapper-net/get-started-with-dapper-net-591592c335aa

    sections "Parameters" and the following one "Stored Procedures". Have fun! :)