0

I have the following problem. I'm facing a NullReferenceException when inserting into Oracle DB. It's thrown by ExecuteNonQuery().

Looked at this related article but doesn't helped: StackOverFlow NullReferenceException

I'm inserting like this:

//Getting connection String Details from other Class
string conn_string = db_conn.connection();

using (var OraConnection = new OracleConnection(conn_string))
{
    OraConnection.Open();
    using (var OraCommand = OraConnection.CreateCommand())
    {
        try
        {
            OraCommandd.BindByName = true;
            for (int i = 0; i < datagrdiview.Rows.Count - 1; i++)
            {
                //Insert is done via CommandText and Parameters
                OraCommand.CommandText = "INSERT INTO TABLE () VALUES ()";

                //Parameter added like this
                OracleParameter param1 = new OracleParameter("param_1", param1_textbox.Text);
                OraCommand.Parameters.Add(param1);

                //Null Reference thrown on line below
                int rows = OraCommand.ExecuteNonQuery();

                if (rows == 0)
                {
                    MessageBox.Show("Not successful", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (rows > 0)
                {
                    MessageBox.Show("Successful", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        catch (OracleException ex)
        {
            switch (ex.Number)
            {
                case 1:
                    MessageBox.Show("F1 - Error inserting Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case 12560:
                    MessageBox.Show("F2 - Database not connectable.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                default:
                    MessageBox.Show("F3 - Database Error: " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("F4 - Error: " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            OraConn.Dispose();
        }
    }
}

Does anyone can help me. What could cause the NullRefernceException (object instance is not set to an object) in OraCommand.

Alexander Petrov
  • 11,401
  • 2
  • 15
  • 45
  • Are you sure CommandText is coorect? According to MS docs, it should be like cmd = new OracleCommand("INSERT INTO Dept (DeptNo, DName) " + "VALUES (:pDeptNo, :pDName)", conn); (https://docs.microsoft.com/en-us/dotnet/api/system.data.oracleclient.oracledataadapter.insertcommand?view=netframework-4.8) – Roman Ryzhiy Aug 26 '20 at 07:56
  • For Select Queries it works fine. I used this code a lot but i try your idea. Thanks @RomanRyzhiy – Simon Mayrhofer Aug 26 '20 at 07:57
  • Are you sure param1_textbox is not empty ? – Wowo Ot Aug 26 '20 at 08:30
  • Yes i checked now and probably found the problem. But can it be that there is a problem with the parameters? – Simon Mayrhofer Aug 26 '20 at 09:13

0 Answers0