First off, never do "anonymous inserts" - always list the columns you want to insert, in the order you want to insert them:
INSERT INTO MyTable (MyColumn1, MyCOlumns2) VALUES (@ValueForColumn1, @ValueForColumn2)
Second, you almost certainly shouldn't be inserting ID values, particularly if they are Row IDs: values which uniquely identify a row that otherwise may contain identical data. Never use the existing maximum and add one yourself - it causes intermittent problems that are a total PITA to fix.
Third, you need to specify a value for each of the parameters in your INSERT statement: my example above has two parameter values (the "@" prefix indicates a parameter value to SQL) so I would need two calls to
AddWithValue
to supply them:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
{
cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
cmd.ExecuteNonQuery();
}
}
You only supply one in your example, but you tell it to expect 4 v alues.
Fourth, The parameter names must match up in the
AddWithValue
call and the INSERT statement: you are missing the "@" prefix in your call.
Fifth, SQL uses commas as separators, not terminators:
(@C1, @C2)
is fine, but
(@C1, @C2,)
is a syntax error as there is nothing between the final comma and the close bracket.