Narjis
Narjis

Reputation: 11

Issue with update with OleDbConnection

I'm trying to create an Update statement with ADO net. I am using Access 2010 and C sharp, but it is not working and it doesn't have me any error.

public static OleDbConnection FileConnexion()
{
    try
    {
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
        "Data Source=..//..//AdoNetTesteRequete//Teste.accdb";

        OleDbConnection conn = new OleDbConnection(connectionString);
        conn.Open();
        return conn;
    }
    catch (Exception e)
    {
        Console.WriteLine("L'erreur suivante a été rencontrée :" + e.Message);
        return null;
    }
}
public void Modifier(Inscription ins)
{
    string req = "UPDATE inscription set  Adresse=@Adresse,Tele=@Tele where Nom=@Nom and Prenom=@Prenom";

    OleDbConnection connexion = FicheConnexion.FileConnexion();

    OleDbCommand conn = new OleDbCommand(req, connexion);

    conn.Parameters.AddWithValue("@Nom", ins.Nom);
    conn.Parameters.AddWithValue("@Prenom", ins.Prenom);
    conn.Parameters.AddWithValue("@Adresse", ins.Adresse);
    conn.Parameters.AddWithValue("@Tele", ins.Tele);
    conn.ExecuteNonQuery();
    connexion.Close();
}

Upvotes: 1

Views: 40

Answers (2)

Coskun Ozogul
Coskun Ozogul

Reputation: 2469

You can check if your where statements returns a row. If there is no error in an update query, it might be simply that there is no corresponding row. In this case ADO.NET will not throw an error.

Check if this returns 0 or 1 :

  string req = "UPDATE inscription set  Adresse=@Adresse,Tele=@Tele where Nom=@Nom and Prenom=@Prenom";

using(OleDbConnection connexion = FicheConnexion.FileConnexion())
 {
   using(OleDbCommand conn = new OleDbCommand(req, connexion))
   {
     conn.Parameters.AddWithValue("@Nom", ins.Nom);
     conn.Parameters.AddWithValue("@Prenom", ins.Prenom);

     int count = Convert.ToInt32( conn.ExecuteScalar());
 }

If the cound is not 0, than you should look for an error.

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176886

There might be issue of sequence of parameter.

string req = "UPDATE inscription set  Adresse=@Adresse,Tele=@Tele where Nom=@Nom and Prenom=@Prenom";

    OleDbConnection connexion = FicheConnexion.FileConnexion();

    OleDbCommand conn = new OleDbCommand(req, connexion);
    conn.Parameters.AddWithValue("@Adresse", ins.Adresse);
    conn.Parameters.AddWithValue("@Tele", ins.Tele);
    conn.Parameters.AddWithValue("@Nom", ins.Nom);
    conn.Parameters.AddWithValue("@Prenom", ins.Prenom);

Add parameter in same sequence as in query , might resolve your issue.

Upvotes: 2

Related Questions