[Solved] Insert multiple rows, count based on another table columns


Here is how you use a parameter in your first insert statement. I think you still have some very serious logic issues here though. This is going to insert 1 row into StockDetail and the values don’t make any sense at all. You would be inserting the count of rows from the Branch table as the BranchID which is probably not what you really want. I suspect what you want is a row in this table for each Branch?

theCommand.CommandText = "INSERT INTO StockDetail(ItemID, BranchID, Price) VALUES(
                         @ItemID,
                         (SELECT COUNT(BranchID) FROM Branch),
                         0
                         )"
theCommand.Parameters.Add("@ItemID", SqlDbType.Varchar).Value = Me.TextBox_ItemID.Text;

I suspect what you really want is something more like this.

theCommand.CommandText = "INSERT INTO StockDetail(ItemID, BranchID, Price) 
                        select @ItemID
                            , BranchID
                            , 0
                        from Branch";
theCommand.Parameter.Add("@ItemID", SqlDbType.Varchar).Value = Me.TextBox_ItemID.Text;

9

solved Insert multiple rows, count based on another table columns