Insert using parameters in the C# application for the Oracle database error: ORA-01036: invalid variable name/number

Asked

Viewed 254 times

0

I noticed that there are many questions about this error but none solved my case the connection works normally, there is the Insert method():

      public bool Insert(ArrayList arrAlunos)
      {
        vsql = "insert into ALUNOS_("
             + "UF"
             + ", IDADE"
             + ", TELEFONE"
             + ", NOME_PAI"
             + ", ENDERECO"
             + ", EMAIL"
             + ", NOME"
             + ", CIDADE"
             + ", NOME_MAE"
               + ") values ("
                        + "@UF"
                        + ", @IDADE"
                        + ", @TELEFONE"
                        + ", @NOME_PAI"
                        + ", @ENDERECO"
                        + ", @EMAIL"
                        + ", @NOME"
                        + ", @CIDADE"
                        + ", @NOME_MAE"
                        + ")";

        if (this.conectar())
        {
            try
            {
                //vsql = "INSERT INTO ALUNOS_ (UF, IDADE, TELEFONE, NOME_PAI, ENDERECO, EMAIL, NOME, CIDADE, NOME_MAE) values ('sp', 0,'','','','','','','')";
                OracleCommand objCmd = new OracleCommand(vsql, objCon);

                objCmd.Parameters.Add(new OracleParameter("@UF",       arrAlunos[0]));
                objCmd.Parameters.Add(new OracleParameter("@IDADE",    arrAlunos[1]));
                objCmd.Parameters.Add(new OracleParameter("@TELEFONE", arrAlunos[2]));
                objCmd.Parameters.Add(new OracleParameter("@NOME_PAI", arrAlunos[3]));
                objCmd.Parameters.Add(new OracleParameter("@ENDERECO", arrAlunos[4]));
                objCmd.Parameters.Add(new OracleParameter("@EMAIL",    arrAlunos[5]));
                objCmd.Parameters.Add(new OracleParameter("@NOME",     arrAlunos[6]));
                objCmd.Parameters.Add(new OracleParameter("@CIDADE",   arrAlunos[7]));
                objCmd.Parameters.Add(new OracleParameter("@NOME_MAE", arrAlunos[8]));

                objCmd.ExecuteNonQuery();                                        

                return true;
            }
            catch (OracleException erro)
            {
                throw erro;
            }
            finally
            {
                this.desconectar();
            }
        }
        else
        {
            return false;
        }

Here is where I pass the values to the array to test the insertion

     public void testDB()
    {
        sisDBAdm obj = new sisDBAdm();
       // Dictionary<String, Alunos> dicAluno = new Dictionary<String, Alunos>();
       ArrayList arr = new ArrayList();
        arr.Add("SP");
        arr.Add(24);
        arr.Add("3432 2222");
        arr.Add("Jose");
        arr.Add("Catarina Martins");
        arr.Add("[email protected]");
        arr.Add("Bruno Viny");
        arr.Add("Votuporanga");
        arr.Add("MAria de Lourdes");

        if (obj.Insert(arr))
        {
            MessageBox.Show("Inserido com sucesso");
        }
        else
        {
            MessageBox.Show("Não inserido");
        }
    }

SOLVED: Just swap @ for : in the parameters, it was like this:

    public bool Insert(ArrayList arrAlunos)
    {
        vsql = "insert into ALUNOS_ ("
             + "UF"
             + ", IDADE"
             + ", TELEFONE"
             + ", NOME_PAI"
             + ", ENDERECO"
             + ", EMAIL"
             + ", NOME"
             + ", CIDADE"
             + ", NOME_MAE"
               + ") values ("
                        + ":UF"
                        + ", :IDADE"
                        + ", :TELEFONE"
                        + ", :NOME_PAI"
                        + ", :ENDERECO"
                        + ", :EMAIL"
                        + ", :NOME"
                        + ", :CIDADE"
                        + ", :NOME_MAE"
                        + ")";

        if (this.conectar())
        {
            try
            {
                //vsql = "INSERT INTO ALUNOS_ (UF, IDADE, TELEFONE, NOME_PAI, ENDERECO, EMAIL, NOME, CIDADE, NOME_MAE) values ('sp', 0,'','','','','','','')";
                OracleCommand objCmd = new OracleCommand(vsql, objCon);

                objCmd.Parameters.Add(new OracleParameter(":UF",       arrAlunos[0]));
                objCmd.Parameters.Add(new OracleParameter(":IDADE",    arrAlunos[1]));
                objCmd.Parameters.Add(new OracleParameter(":TELEFONE", arrAlunos[2]));
                objCmd.Parameters.Add(new OracleParameter(":NOME_PAI", arrAlunos[3]));
                objCmd.Parameters.Add(new OracleParameter(":ENDERECO", arrAlunos[4]));
                objCmd.Parameters.Add(new OracleParameter(":EMAIL",    arrAlunos[5]));
                objCmd.Parameters.Add(new OracleParameter(":NOME",     arrAlunos[6]));
                objCmd.Parameters.Add(new OracleParameter(":CIDADE",   arrAlunos[7]));
                objCmd.Parameters.Add(new OracleParameter(":NOME_MAE", arrAlunos[8]));

                objCmd.ExecuteNonQuery();                                        

                return true;
            }
            catch (OracleException erro)
            {
                throw erro;
            }
            finally
            {
                this.desconectar();
            }
        }
        else
        {
            return false;
        }
  • because you don’t create an object instead of passing an array ?

  • I’m just doing tests, I started in C# this week. I’m following a little tutorial

  • got it, take _ out of the @NOME_PAI" parameters see hint .. https://answall.com/a/27426/43340

1 answer

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.