coscho

coscho

  • NA
  • 3
  • 3.9k

Insert data in Access database with id = primary key

Apr 14 2011 3:01 AM
Hello all

My code do not insert data in Access database.
I have a table 'firme' and a field 'id' = primary key , autonumber.

try
  {
  conn.Open();

  string den = textBoxDenumire.Text;
  int cui = Convert.ToInt32(textBoxCui.Text);
  string reg = textBoxRegCom.Text;
  string adr = textBoxAdresa.Text;
  string jud = comboBoxJudet.Text;
  string tel = textBoxTelefon.Text;
  da.InsertCommand = new OleDbCommand("INSERT INTO firme(denumire, cui, j, adresa, judet, telefon) VALUES('" + den + "', '" + cui + "', '" + reg + "', '" + adr + "', '" + jud + "', '" + tel + "')", conn);
 
  //da.InsertCommand = new OleDbCommand("INSERT INTO firme(denumire, cui, j, adresa, judet, telefon) VALUES(@DENUMIRE,@CUI,@J,@ADRESA,@JUDET,@TELEFON)", conn);
  //da.InsertCommand.Parameters.Add("@DENUMIRE", OleDbType.VarChar).Value = textBoxDenumire.Text;
  //da.InsertCommand.Parameters.Add("@CUI", OleDbType.Integer).Value = textBoxCui.Text;
  //da.InsertCommand.Parameters.Add("@J", OleDbType.VarChar).Value = textBoxRegCom.Text;
  //da.InsertCommand.Parameters.Add("@ADRESA", OleDbType.VarChar).Value = textBoxAdresa.Text;
  //da.InsertCommand.Parameters.Add("@JUDET", OleDbType.VarChar).Value = comboBoxJudet.Text;
  //da.InsertCommand.Parameters.Add("@TELEFON", OleDbType.VarChar).Value = textBoxTelefon.Text;
 
  da.InsertCommand.ExecuteNonQuery();
 
  conn.Close();

  MessageBox.Show("Record updated successfully");
  }

  catch (System.Data.Odbc.OdbcException ex)
  {
  MessageBox.Show(ex.ToString());
  }

the message "Record updated successfully" works.
any help is very usefull.
thanks

Answers (4)