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
.