Query works in the database but not via code

Asked

Viewed 4,343 times

2

using (var cnx = new OleDbConnection(new AdministradorDAO().conexao))
            {
                var sql =
            @"insert into usuarios(matricula, nome, senha, nivel, maleta, email) 
            values (@matricula, @nome, @senha, @nivel, @maleta, @email)";


                using (var cmd = new OleDbCommand(sql, cnx))
                {
                    cnx.Open();

                    cmd.Parameters.AddWithValue("@matricula", txt_matricula.Text);
                    cmd.Parameters.AddWithValue("@nome", txt_nome.Text);
                    cmd.Parameters.AddWithValue("@senha", txt_senha.Text);
                    cmd.Parameters.AddWithValue("@nivel", DropDownList_nivel.Text);
                    cmd.Parameters.AddWithValue("@maleta", txt_maleta.Text);
                    cmd.Parameters.AddWithValue("@email", txt_email.Text);

                    try { cmd.ExecuteNonQuery(); }
                    catch { }
                    finally { if (cnx.State == ConnectionState.Open) cnx.Close(); }
                }
            }

When making the Insert directly on the seat, it rotates perfectly

insert into actweb.usuarios (matricula, nome, senha, nivel, maleta, email) values ('TESTE', 'Frederico', 'TESTE', 1, 7000, '[email protected]'); 

I running directly, without using the right Parameters.

var sql = @"Insert into usuarios(id,name) values (ID_USUARIOS.nextval, 'It will work')";

  • Insert into actweb.usuarios (matricula, nome, password, nivel, maleta, email) values ('TESTE', 'Frederico', 'TESTE', 1, 7000, '[email protected]');

  • You should [Edit] your question to add this information. You know this try catch finnaly does not have any utility in your code? It is even causing problem. The using is solving connection closure.

  • 1

    The catch is swallowing the error. If you take it out, you will have some useful information to figure out where the error is. Then you can solve or post here what the error is, so it is easier to help you.

  • I took the catch, now you are showing this error One or more errors occurred while processing the command. ORA-00936: expression not found

  • You need to [edit] your question to make it complete with this information and make it easy for anyone who knows Oracle errors and can help you.

1 answer

4

There are two things wrong with your code:

  • try with catch emptiness;
  • As I recall, the syntax for insertion in Oracle does not start with @, and yes by two-points;

The right thing would be something like this:

using (var cnx = new OleDbConnection(new AdministradorDAO().conexao))
{
    var sql =
        @"insert into usuarios(matricula, nome, senha, nivel, maleta, email) 
        values (:matricula, :nome, :senha, :nivel, :maleta, :email)";

    using (var cmd = new OleDbCommand(sql, cnx))
    {
        cnx.Open();

        cmd.Parameters.AddWithValue("matricula", txt_matricula.Text != "" ? txt_matricula.Text : DbValue.Null);
        cmd.Parameters.AddWithValue("nome", txt_nome.Text);
        cmd.Parameters.AddWithValue("senha", txt_senha.Text != "" ? txt_senha.Text : DbValue.Null);
        cmd.Parameters.AddWithValue("nivel", DropDownList_nivel.Text != "" ? DropDownList_nivel.Text : DbValue.Null);
        cmd.Parameters.AddWithValue("maleta", txt_maleta.Text != "" txt_maleta.Text : DbValue.Null);
        cmd.Parameters.AddWithValue("email", txt_email.Text != "" ? txt_email.Text : DbValue.Null);

        try 
        { 
            cmd.ExecuteNonQuery(); 
        }
        catch (Exception e) 
        {
            throw; 
        }
        finally 
        { 
            if (cnx.State == ConnectionState.Open) cnx.Close(); 
        }
    }
}
  • Placing the two-points, displays the following message: ORA-01008: not all variables are limited. I looked at the meaning and speech that occurs when some of the parameters have not been informed.However in my code there is no parameter that has not been informed.

  • I can’t find the solution to my problem !

  • Well, this new error indicates that something is going null, which you can’t. Can you tell me which field is?

  • in my test I am passing all fields, plus the only required fields is ID, and NAME. However the ID is auto-increment.

  • @Frederico So it’s wrong. You have to say when it’s null. I’ll edit the answer.

  • adding Dbvalue, from this message: The name 'Dbvalue' does not exist in the Current context

  • have any suggestions you can give me? Remembering that my database is an oracle 10g

  • You correctly added the reference to DbValue, as described here? http://msdn.microsoft.com/en-us/library/system.dbnull.value(v=vs.110). aspx

Show 3 more comments

Browser other questions tagged

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