Stack Exchange
Managing Sellings
My code needs to save all the information captured in the WinForm Vendita.
I'm using the following code but it doesn't save the information in the DB and gives me an error indicating a data type mismatch in the criteria expression. 
I am using the following code:
    System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand();
    System.Data.OleDb.OleDbCommand cmd2 = new System.Data.OleDb.OleDbCommand();
    System.Data.OleDb.OleDbCommand cmd3 = new System.Data.OleDb.OleDbCommand();
    cmd1.CommandType = System.Data.CommandType.Text;
    cmd2.CommandType = System.Data.CommandType.Text;

    cmd1.CommandText = "INSERT INTO FattureVoci ([IDVoce],[CodiceArticolo],[Descrizione],[Quantita],[PrezzoUnitario]) VALUES (@Id,@Prod,@Descr,@Qta,@Prezzo)";
    cmd2.CommandText = "INSERT INTO Fatture ([Intestatario],[PartitaIVA]) VALUES (@Intest,@Iva)"; 

    cmd1.Parameters.AddWithValue("@Prod", this.Prodotto.Text);

    cmd1.Parameters.AddWithValue("@Iva", Convert.ToInt32(this.PartitaIVA.Text));
    cmd1.Parameters.AddWithValue("@Descr", this.Descrizione.Text);
    cmd1.Parameters.AddWithValue("@Qta", Convert.ToInt32(this.Qta.Text));
    cmd1.Parameters.AddWithValue("@Intest", this.Intestatario.Text);
    cmd1.Parameters.AddWithValue("@Id", Convert.ToInt32(this.id.Text));
    cmd1.Parameters.AddWithValue("@Prezzo", Convert.ToInt32(this.Prezzo.Text));


    cmd1.Connection = conn;
    conn.Open();
    cmd1.ExecuteNonQuery();
    cmd2.ExecuteNonQuery();
    conn.Close();
}



There is too much wrong with your code. First, you don't provide cmd2 with parameters. Also, your question is unclear and lacks a decent error message.

On which line exactly? What is your column types? And you didn't parameters to your cmd2 before your execute it.

This question as of now is barely answerable. Some hints to improve: (1) provide actual exception name, message and stacktrace; (2) reduce your code to include only relevant data (e.g. don't need all the params listed); (3) database table schema could also help.

Don't close your connection in the catch clause, use the finally clause for that.

@J0HN my Access 2003 is in Italian, I dont think it would help so much

@Kirito I believe keywords are still in english. An I actually don't care about meaning of column names, it's just their spelling and data types are improtant

@J0HN , i just edited the question providing the name of the fields and their types.

As @Patrick has said, you don't seem to be setting the parameters for the second insert with cmd2.Parameters.AddWithValue(...) statements.

@Turophile I edited it but still not working

Iva and Interest belong to cmd2 instead of cmd1. Change this:
cmd1.Parameters.AddWithValue("@Iva", Convert.ToInt32(this.PartitaIVA.Text));
cmd1.Parameters.AddWithValue("@Intest", this.Intestatario.Text);

To:
cmd2.Parameters.AddWithValue("@Iva", Convert.ToInt32(this.PartitaIVA.Text));
cmd2.Parameters.AddWithValue("@Intest", this.Intestatario.Text);

Also, you are using OleDb, so probably Ms Access: you should provide the parameters in the correct order.

Just tried, still not working

@Kiirito: already updated the answer. Check the last sentence.

I do use Ms Access, so it means that i cannot skip any field?

You can, but you have to deliver the fields in the order of the query.

Check the question, i provided the name of the fields and their order, still I should be able to do my query

No, the query order is @Id,@Prod,@Descr,@Qta,@Prezzo. You add the parameters in this order: Prod, Desc, Qta, Id, Prezzo.

That was the mistake ! I get another error now but I might solve it :) thank you !
