Page 1 of 1

Why is the OracleDataReader that I'm using null?

Posted: Fri 06 Apr 2012 00:31
by bclayshannon
With this code (DotConnect Oracle components):

Code: Select all

public List GetApplications(long userABCID) {
       List result = new List();
  try {
    using (OracleCommand command = con.CreateCommand()) {
      command.CommandType = System.Data.CommandType.StoredProcedure;
      command.CommandText = "REPORT.ApplicationPermission.GetApplicationCursor";
      // I tried getting just the Application name this way, too:
      //command.CommandText = "REPORT.ApplicationPermission.ApplicationString";
      command.ParameterCheck = true;
      command.Prepare();
      command.Parameters["iWorkerABCID"].Value = userABCID;

using (OracleDataReader reader = ((OracleCursor)command.Parameters["Result"].OracleValue).GetDataReader()) {
      while (reader.Read()) {
        result.Add(reader.GetString("AppName").ToLower());
      }
    }
...I'm getting "Object reference not set to an instance of an object"

Posted: Fri 06 Apr 2012 12:13
by Pinturiccio
bclayshannon wrote:using (OracleDataReader reader = ((OracleCursor)command.Parameters["Result"].OracleValue).GetDataReader())
When you create parameters for your command they are not initialized. You have initialized iWorkerABCID parameter, but not the Result parameter. I do not know how your procedure works, but I assume, that 'Result' is an out parameter. And according to the logic of your code you want to get data from the database. In this case such parameter can be initialized, if you execute command.ExecuteNonQuery() before declaring OracleDataReader.

It is also necessary to add the following code before executing the command:

Code: Select all

command.Parameters["Result"].Direction = ParameterDirection.Output;

Is declaring "Result" as an Output param really necessary?

Posted: Fri 06 Apr 2012 16:08
by bclayshannon
Isn't this unnecessary if you call

Code: Select all

command.Prepare();
?

Posted: Mon 09 Apr 2012 10:21
by Pinturiccio
When you perform command.ParameterCheck = true; - the required parameters will be added and redundant parameters will be deleted after executing

Code: Select all

command.Prepare();
//or
command.ExecuteNonQuery();
command.Prepare(); creates a prepared (or compiled) version of the command on the server, but does not execute it.
For more information, please refer to
http://www.devart.com/dotconnect/oracle ... epare.html
If you want to receive some value in the "Result" parameter then you should execute the following code:

Code: Select all

command.ExecuteNonQuery();