'String or binary data would be truncated when input data from winforms

I'm trying to insert record into my database through a form made by Winforms. At first I try to insert it directly in SQL Server:

INSERT INTO item (name, price, description, notice, realease_date, due_date, producer, category_id, user_id) 
VALUES ('test', 10000, 'test', 'test', '2022-05-15', '2022-09-14', 'Producer 1', (SELECT id FROM category WHERE name = 'Name 1'), 1)

and it works perfectly as my expect, the data is entered into the database with no problems.

But when I try to input the same data from my Winforms app, I got the error.

This is how I try to do it in C#: first, I have my Item class:

public class Item
{
        public int id { get; set; }
        public string name { get; set; }
        public string? description { get; set; }
        public string? notice { get; set; }
        public int price { get; set; }
        public DateTime realease_date { get; set; }
        public DateTime due_date { get; set; }
        public string producer { get; set; }
        public Category category { get; set; } 
        public string category_name { get; set; }
        public int category_id { get; set; }
        public int user_id { get; set; }
        public User user { get; set; }
}

I declare the query:

public static String SQL_Create = "Insert into  \"Item\"(name, description, notice, price, realease_date, due_date, producer, category_id, user_id)" +
            " values(@name, @description, @notice, @price, @realease_date, @due_date, @producer," +
            "(select id from category where name = @category_name), " +
            " @user_id)";

This is my PrepareCommand and the Insert method:

private static void PrepareCommand(SqlCommand command, Item Item)
{
    command.Parameters.AddWithValue("@item_id", Item.id);
    command.Parameters.AddWithValue("@name", Item.name);
    command.Parameters.AddWithValue("@description", Item.description == null ? DBNull.Value : (object)Item.description);
    command.Parameters.AddWithValue("@notice", Item.notice == null ? DBNull.Value : (object)Item.notice);
    command.Parameters.AddWithValue("@price", Item.price);
    command.Parameters.AddWithValue("@realease_date", Item.realease_date);
    command.Parameters.AddWithValue("@due_date", Item.due_date);
    command.Parameters.AddWithValue("@producer", Item.producer);
    command.Parameters.AddWithValue("@category_name", Item.category_name);
    command.Parameters.AddWithValue("@category_id", Item.category_id);
    command.Parameters.AddWithValue("@user_id", Item.user_id);
}

public static int Insert(Item Item, Database pDb = null)
{
    Database db;

    if (pDb == null)
    {
        db = new Database();
        db.Connect();
    }
    else
    {
        db = (Database)pDb;
    }

    SqlCommand command = db.CreateCommand(SQL_Create);
    PrepareCommand(command, Item);

    int ret = db.ExecuteNonQuery(command);

    if (pDb == null)
    {
        db.Close();
    }

    return ret;
}

The button triggering the Insert method on my form:

private void insertItemBtn_Click(object sender, EventArgs e)
{
     Database db = new Database();
     db.Connect();

     Item item = new Item();
     item.name = iNameTbx.ToString();
     item.notice = iNotTbx.ToString();
     iPriceTbx.Text = item.price.ToString();
     item.description = iDesTbx.ToString();
     item.realease_date = resTimePkr.Value;
     item.due_date = dueTimePkr.Value;
     item.producer = iProducerTbx.ToString();
     item.category_name = iCatTbx.ToString();
     item.user_id = 1;

     ItemTable.Insert(item, db);

     db.Close();    
 }

This is the data I input into my form, exactly like the data I have entered directly to the database, the release date and due date are different but that should not matter, and the Save button will call the insertItemBtn_Click method.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source