0

I had make this small method to insert data from C# forms into my Oracle database. The code processed fine, but when I go to SQL Developer to check if the record has been inserted or not, I found nothing...

        public void conn2db()
        {
            try
            {
                string connstring = "data source=test_db;user id=system;password=password;";
                string statmentcmd = "insert into register_user (userid,username,pass,fullname,phonenum,gender,country) values (" + 1 + "," + textBox1.Text + "," + textBox2.Text + "," + textBox4.Text + "," + textBox5.Text + "," + radioButtonValue+ ","+comboBox1.Text+");";

                OracleConnection conn = new OracleConnection(connstring);
                conn.Open();
                MessageBox.Show("connected to database");

                OracleCommand cmd = new OracleCommand();

                cmd.CommandText=statmentcmd;
                cmd.Connection=conn;
                OracleDataAdapter oda = new OracleDataAdapter(cmd);

                MessageBox.Show(statmentcmd);

                conn.Close();
                MessageBox.Show("Connection closed");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString()); 
            }
        }
2
  • 1
    You should escape the strings or better, use parameters in your command. Commented Nov 23, 2013 at 23:46
  • Problem solved..its coused by TNS services was not running at the services control panel >> system and secuirty >>adminstrative tools>>Computer Management >> services but I am still had problem with insert command line string statementcmd = "insert into register_user (pid,username,pwd,fullname) values ("+'1'+"," + textBox1.Text + "," + textBox2.Text + "," + textBox4.Text + ")"; Error message number is ora-00984 column not allowed here could anyone check it for me ?? is it double qoutes or single qoutes ? or what is it please thanks Commented Nov 24, 2013 at 2:25

2 Answers 2

1

Try executing the command like this:

OracleCommand cmd = new OracleCommand();

cmd.CommandText = statmentcmd;
cmd.Connection = conn;
cmd.ExecuteNonQuery();

Or more simply:

OracleCommand cmd = new OracleCommand(statmentcmd, conn);
cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

3 Comments

it will throw an exception
@xXghostXx What exception do you see?
@xXghostXx that exception indicates there's something wrong in your command string. I strongly suggest using a parameterized command instead. That makes it easier to catch errors in the SQL syntax and protects you from SQL injection attacks.
0

try change the Copy to Output Directory : Do notcopy

2 Comments

more explanation please ?
This answer is a start, but more information would make it a great answer not just for the person asking, but for others who may run into this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.