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
Loading
Suthish NairPosted Apr 14, 2011, 7:28 AM
Suthish NairPosted Apr 14, 2011, 3:24 PM
coschoPosted Apr 14, 2011, 11:21 AM
thank you for quick response.
I am learning right now c#. It was my mistake.
There was no error. Just fill the datagridview.
the problem was that the database was in wrong directory.
this works !!!
try
{
conn.Open();
da.InsertCommand = new OleDbCommand("INSERT INTO firme(denumire, cui, j, adresa, judet, telefon) VALUES(@DENUMIRE,@CUI,@J,@ADRESA,@JUDET,@TELEFON)", conn);
da.InsertCommand.Parameters.AddWithValue("@DENUMIRE", textBoxDenumire.Text);
da.InsertCommand.Parameters.AddWithValue("@CUI", Convert.ToInt32(textBoxCui.Text));
da.InsertCommand.Parameters.AddWithValue("@J", textBoxRegCom.Text);
da.InsertCommand.Parameters.AddWithValue("@ADRESA", textBoxAdresa.Text);
da.InsertCommand.Parameters.AddWithValue("@JUDET", comboBoxJudet.Text);
da.InsertCommand.Parameters.AddWithValue("@TELEFON", textBoxTelefon.Text);
da.InsertCommand.ExecuteNonQuery();
conn.Close();
this.firmeTableAdapter.Fill(this.NanoDataSet.firme);
MessageBox.Show("Firma a fost adaugata cu succes", "Nano", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Data.Odbc.OdbcException ex)
{
MessageBox.Show(ex.ToString());
}
thank you
Prabhakar PariharPosted Apr 14, 2011, 4:40 AM