-1

I've a problem to update my database.

public void UpdateAto(AtoEntity atoEntity)
{
    OracleDataAdapter da = new OracleDataAdapter();

    string oradb = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=blabla.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test13)));;User Id=id13;Password=pwd13;";
    OracleConnection con = new OracleConnection(oradb);
    con.Open();

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = con;

    cmd.CommandText = 
        "UPDATE SLAPTEOP SET XOCL ='" + atoEntity.OidOcl +
                         "',NOM='" + atoEntity.NameAto +
                         "',ETAT='-1" +
                         "',NBPAAN='11" +
                         "',DESCRIPTION='" + atoEntity.DescriptionAto +
                         "',APPORTEUR='" + atoEntity.ContributorAto +
                         "',AUTEUR_MISEAJOUR='" + atoEntity.AuthorUpdateAto +
                          "',DATE_MISEAJOUR=SYSDATE WHERE OID ='" + atoEntity.OidAto + "'";

    cmd.CommandType = CommandType.Text;

    cmd.ExecuteNonQuery();
    con.Dispose();
}

The request is good, it was written without parameters to make it simpler. It has been tested on the SGBD. Select and Insert Request works too.

When I execute cmd.ExecuteNonQuery() nothing happens in database. It turns in loop.

A try / catch block had been added but no errors or exceptions were caught.

CDspace
  • 2,639
  • 18
  • 30
  • 36
Nitneuq
  • 99
  • 3
  • 12
  • 3
    This is very unclear. What does POST or GET have to do with this? Trap exceptions and log or debug them. The Exception details will tell you what is going wrong. And please use parameterized queries to prevent sql injection and syntax errors. – Crowcoder Dec 28 '17 at 13:51
  • `cmd.ExecuteNonQuery` should return the number of Updated rows. May you warp the things into a try catch and [edit] your exception into your question? – Drag and Drop Dec 28 '17 at 13:52
  • [related question](https://stackoverflow.com/questions/28487662/c-sharp-oracle-dataaccess-client-not-responding-executing-sql-update-stateme?noredirect=1&lq=1) – Drag and Drop Dec 28 '17 at 13:56
  • 1
    `OracleDataAdapter da` is not required. – Wernfried Domscheit Dec 28 '17 at 13:57
  • May you make a simple Select to check if the connection string is ok? – Drag and Drop Dec 28 '17 at 14:23
  • May you try `OracleCommand cmd = con.CreateCommand();` instead of `OracleCommand cmd = new OracleCommand();` – Drag and Drop Dec 28 '17 at 14:26
  • yes, SELECT and INSERT works! I just have a problem with UPDATE.... – Nitneuq Dec 28 '17 at 14:27
  • 1
    The ExecuteNonQuery returns 0 if no rows are updated or >0 if the command has modified some rows. Please check the return value of ExecuteNonQuery. If it is zero then the WHERE clause failed to find a record to update. Meaning that the value you use for Oid is wrong (Are you sure that this OID is a string?) – Steve Dec 28 '17 at 14:28
  • J'ai bien compris nitneuq, la question est est ce que ça marche sur cette object commande pas si ça marche dans une autre partie du code. On sait jamais un probleme de copier coller ça arrive. (French version to tone down) – Drag and Drop Dec 28 '17 at 14:31
  • i can not see the value of a because the program does not stop on ExecuteNonQuery .. – Nitneuq Dec 28 '17 at 14:35
  • C'est la première fois que je fais un update sur cet objet insert OK, select OK, update CRASH. alors que la requête SQL est correcte – Nitneuq Dec 28 '17 at 14:37

2 Answers2

0

You should use bind variables. Check number of updated rows like this:

cmd.CommandType = CommandType.Text;
cmd.CommandText = 
   "UPDATE SLAPTEOP SET XOCL = :xocl, 
       NOM = :mom, ETAT = -1, 
       NBPAAN = 11, DESCRIPTION = :desc, 
       APPORTEUR = :apporteur, AUTEUR_MISEAJOUR = :auteur, 
       DATE_MISEAJOUR = SYSDATE
    WHERE OID = :oid";

cmd.Parameters.Add("xocl", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidOcl;
cmd.Parameters.Add("mom", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.NameAto;
cmd.Parameters.Add("desc", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.DescriptionAto;
cmd.Parameters.Add("apporteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.ContributorAto;
cmd.Parameters.Add("auteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.AuthorUpdateAto;
cmd.Parameters.Add("oid", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidAto;

var r = cmd.ExecuteNonQuery();

Console.WriteLine("{0} rows updated", r);
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
0

Drag and Drop I don't see the problem...

   cmd.CommandType = CommandType.Text;
            cmd.CommandText = 
                "UPDATE SLAPTEOP SET XOCL = :xocl,"+ 
            "NOM = :nom, ETAT = -1,"+
            "NBPAAN = 11, DESCRIPTION = :desc,"+ 
            "APPORTEUR = :apporteur, AUTEUR_MISEAJOUR = :auteur,"+
            "DATE_MISEAJOUR = SYSDATE"+
            "WHERE OID = :oid";

            cmd.Parameters.Add("xocl", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidOcl;
            cmd.Parameters.Add("nom", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.NameAto;
            cmd.Parameters.Add("desc", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.DescriptionAto;
            cmd.Parameters.Add("apporteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.ContributorAto;
            cmd.Parameters.Add("auteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.AuthorUpdateAto;
            cmd.Parameters.Add("oid", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidAto;
Nitneuq
  • 99
  • 3
  • 12