The two following samples of C# code, writing info to an Access database, as far as I can tell, SHOULD produce the same output. However the first one works, and the second gives an error.
The first code: Without an AutoNumber primary key field, works great. But only until I try to add a row where any field is not different. I HAVE to have the AUTONUMBER unique ID, (for the obvious reason of a lack of unquity)
string vsql = string.Format("insert into Log values " +
"('{0}','{1}',{2},{3},'{4}',#{5}#,'{6}')",
comboBox1.Text,
comboBox2.Text,
int.Parse(textBox1.Text),
int.Parse(textBox1.Text),
textBox3.Text,
textBox2.Text,
addRemove
);
The second code: returns the error message:
"Additional information: Number of query values and destination fields are not the same."
As far as I can see, they both have the same number of fields. And still, neither has a unique AUTONUMBER ID field, which I can't add because I don't know how to make the code "insert" or "not insert" anything into the autonumbering field. Again, I obviously need the field. ANY HELP IS APPRECIATED! Either code is acceptable, as long as I have an autonumber field that will update itself, when my form submits a new record.
string vsql = string.Format("INSERT INTO Log (" +
"Location, " +
"Drug, " +
"Quantity, " +
"Strength, " +
"Initials, " +
"'Date'," +
"add_Remove" +
") VALUES (" +
comboBox1.Text,
comboBox2.Text,
int.Parse(textBox1.Text),
int.Parse(textBox1.Text),
textBox3.Text,
textBox2.Text,
addRemove);
Try this: right after your insert sql command, execute another sql call:
SELECT @@IDENTITY
The result returned will be the autonumber field associated with the record that was just added. Your original insert sql command should NOT be attempting to insert the autonumber field value. Just omit it.
Also, your second sql text has a problem. Reformat it to look like this:
string vsql = string.Format("insert into Log (Location,Drug,Quantity,"+
"Strength,Initials,Date,add_Remove values" +
"('{0}','{1}',{2},{3},'{4}',#{5}#,'{6}')",
comboBox1.Text,
comboBox2.Text,
int.Parse(textBox1.Text),
int.Parse(textBox1.Text),
textBox3.Text,
textBox2.Text,
addRemove
);