0

I'm trying to Insert data into ms access database from textboxes ,as I run the program it kinda works ,data is shown in the data grid but it is not saved in the database

i'm using two methods : loadDb to load the data base & InsertInto() : to insert data and a connection.cs to create connexion

here's my code

public DataTable LoadDb()
    {
        connexion.con.Open();
        DataTable dt = new DataTable();
        String query = "SELECT * FROM vehicule";
        OleDbCommand cmd = new OleDbCommand(query, connexion.con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        da.Fill(dt);
        connexion.con.Close();
        return dt; 
    }

    public void InsertInto()
    {

        try
        {
            connexion.con.Open();

            OleDbCommand cmd = new OleDbCommand(); 

            cmd.Connection = connexion.con; 
            cmd.CommandText = "INSERT INTO vehicule (id_vehicule,n_carte_grise_veh, marque_veh, annee_fab_veh, couleur_veh, nombre_place_veh, matricule_veh, libelle_vehicule, statut_vehicule) VALUES ( '" + id_vehiculeTextBox.Text + "','" + n_carte_grise_vehTextBox.Text + "','" + marque_vehTextBox.Text + "','" + annee_fab_vehTextBox.Text + "','" + couleur_vehTextBox.Text + "','" + nombre_place_vehTextBox.Text + "','" + matricule_vehTextBox.Text + "','" + libelle_vehiculeTextBox.Text + "','" + statut_vehCB.Text + "')";


            cmd.ExecuteNonQuery();

            connexion.con.Close();
            MessageBox.Show("Ajouté Avec Succès !!!");
            dataGridView1.DataSource = LoadDb(); 
        }
        catch (Exception ex)
        {
            connexion.con.Close();
            MessageBox.Show(ex.Message);
        }

    }
private void button2_Click(object sender, EventArgs e)
    {
        InsertInto();

    }

As loaded:

enter image description here

But in DB:

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills Jun 09 '18 at 03:56
  • So data is shown in the grid after its inserted or not? There's not enough info. What's the error? Is it the right db Connection String? – Jeremy Thompson Jun 09 '18 at 04:05
  • __Do not__ call a `DataGridView`a `GridView` or a `DataGrid` and vice versa!! This is wrong and confusing as those are different controls. Always call things by their __right__ name! - So ,are you saying you can load the saved data but they are not there?? That would be a little strange. – TaW Jun 09 '18 at 06:57
  • @JeremyThompson yes it is inserted in the datagridview but not in the data tables in the mc access data base,and yes it is the same db connection string i check it –  Jun 09 '18 at 08:39
  • @TaW i didnt know that ,sorry ! it is a datagridview ,and i can load the data in but it is not saved in the database –  Jun 09 '18 at 08:42
  • So to repeat: You can load from the same table where you save? And they still are not there? The latter should not be possible. How do you test? Maybe your test tool uses cached data? – TaW Jun 09 '18 at 08:53
  • @TaW yes here's the screen shots here i inserted the 2nd one (id_vehicule = 2 ) ibb.co/ndK2ho and here it is not saved ibb.co/kGqQ2o the "id_vehicule = 1 i inserted it through ms access not useing the textboxes" –  Jun 09 '18 at 09:30
  • Hm; I have added the images. Could it be that there is a 'update' or 'reload' command in VS? Also: What happens when you close and reopen VS: Are the data still in the DGV and still not in the DB? That could only mean you are not using the same DB: Test by adding a row externall and see where it appears..! – TaW Jun 09 '18 at 09:39
  • Debug the code, watch a video on debugging. Check your insert works, because you're not using parameterized commands it's possible a comma or other reserved character is the problem. – Jeremy Thompson Jun 09 '18 at 10:18
  • yes there is a 'update' command but before i added it it didnt work either. when i close and reopen VS nothing happens ,the problem is still there @TaW –  Jun 09 '18 at 11:07

0 Answers0