I am attempting to insert an int value in a table (RamResults) on a database, the db structure is as follows:
Table Columns:
ID (Primary Key)
Results (int type)
In C# I attempt to insert a value into the Results column:
// Retrieve the connection string from the settings file.
string conString = Properties.Settings.Default.MyDBConnectionString;
// Open the connection using the connection string.
using (SqlCeConnection con = new SqlCeConnection(conString))
{
con.Open();
// Insert into the SqlCe table. ExecuteNonQuery is best for inserts.
int num = 5;
using (SqlCeCommand com = new SqlCeCommand(
"INSERT INTO RamResults VALUES(@num)", con))
{
com.Parameters.AddWithValue("@num", num);
com.ExecuteNonQuery();
}
}
But I get the following error:
The number of columns in the query and the table must match. [ Number
of columns in query = 1, Number of columns in table = 2 ]
I was under the impression that the primary key (ID) did not need any data inserted into it, as it would automatically assign an ID number in that column?
Edit:
Assuming that I need to autoincrement the PK, I can’t see how to do this when editing the table schema in VS 2010:

In your case seemes to that ID is not IDENTITY autoincrementing value and you should specify its value explicitly
and you, of course, should provide the @id parameter’s value manually
OR
Should place the
DEFAULTto the Id column, in your case – orNEWID()orNEWSEQUENTIALID()In this case your query should look like this:
OR without
DEFAULTconstraint: