My code:
int total = Convert.ToInt32(form["REC-tr-total"]);
int value = 0;
string insValOutput = "";
if (total > 0)
{
for (int i = 1; i <= total; i++)
{
value = Convert.ToInt32(form["inventoryID[" + i + "]"]);
insValOutput = $"(" + vCAS_capture_payments.Id + ", value{i + 1}),";
}
db.Database.ExecuteSqlCommand(String.Format(@"INSERT INTO dbo.VCAS_capture_payments__REF_items (FK_capture_paymentsId, FK_inventoryId) VALUES {0};", insValOutput));
}
The error message:

This seems like a pretty straight-forward loop, but error keeps coming up... Experts please advise.
UPDATE: for those asking, I've updated this line as follows:
insValOutput = String.Format(@"({0},{1}),", vCAS_capture_payments.Id, value); // (1, value1),(2, value2) ...
But I get the same results.
UPDATE No.2:
Thanks for all the comments, really appreciate it. I know the code is not the best, but I have been fighting with how best to implement multi-insert via LinQ and Entity Framework in my current codebase.
However from the answer provided, I was able to amend the code as follows, which works.
The culprit was the SQL statement and the concatenation.
int total = Convert.ToInt32(form["REC-tr-total"]); // -- Total Receipt item rows
int value = 0;
string insValOutput = ""; // -- Insert VALUES variable
if (total > 0)
{
for (int i = 1; i <= total; i++)
{
value = Convert.ToInt32(form["inventoryID["+i+"]"]); // -- Fetch the inventory ID for index i
insValOutput += String.Format(@"({0},{1}),", vCAS_capture_payments.Id, value); // (1, value1),(2, value2) ...
}
db.Database.ExecuteSqlCommand(String.Format(@"
INSERT INTO dbo.VCAS_capture_payments__REF_items (FK_capture_paymentsId, FK_inventoryId)
VALUES {0};", insValOutput.TrimEnd(',', ' ')));
}
insValOutput = $"(" + vCAS_capture_payments.Id + ", value{i + 1}),";- this certainly doesn't do what you expect it to. And I guess it's the cause for the error at hand. But there are a couple more issues with this code.(<whatever vCAS_capture_payments.Id is>, value{i + 1}),'); drop table Users; --db? If it's a DbContext, why execute raw SQL like this? In any case, if you don't want to create a SqlCommand with parameters, consider using Dapper which automatically parameterizes queries based on object properties. You can even insert a list of itemsconnection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", items);