Question

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);
Was it helpful?

Solution

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
     );

OTHER TIPS

I'm not sure if you're using String.Format correctly

Try to evolve this code:

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);

Note: I haven't tested Edit: added a missing coma

I searched the web for some time, trying to wrap my head around the responses I got. All of which were great suggestions. This was a great example of "Occam's Razor". I inserted the following before the "string vsql" line, and it works like a charm.

Thanks to all that responded!

OleDbCommand dbCommand;
OleDbDataReader dbReader;
new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\MyDatabase.accdb");
dbCommand = new OleDbCommand("select count(*) as Record_Count from Log", vcon);
dbReader = dbCommand.ExecuteReader();
if (dbReader.Read() == true)
    rowCount = dbReader["Record_Count"].ToString();
else
    return;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top