Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to retrieve a BLOB value from a oracle database using a stored procedure with output parameters(parm). Here is the piece of code where I do this.
C#
using (DbCommand cmd = db.GetStoredProcCommand(sStoredProcName))
{
  cmd.Connection = conn;
  AttachParameters(db, cmd, lParamValueList);

  // Open connection
  if (idbTransaction != null)
  {
    db.ExecuteNonQuery(cmd, idbTransaction);
  }
  else
  {
    db.ExecuteNonQuery(cmd);
  }
  // Retrieve any return values
  GetReturnValues(db, conn, cmd, ref lParamValueList);
}


And then under GetReturnValues there is another calling function, the piece of troubled code is

C#
cmd.Connection = conn;
if (db != null && cmd != null && (Out || IsReturnValue))
{
  int iIndex = ((Oracle.DataAccess.Client.OracleCommand)cmd).Parameters.IndexOf(ParamName);
  if (iIndex >= 0)
  {
    Oracle.DataAccess.Client.OracleParameter parm = (((Oracle.DataAccess.Client.OracleCommand)cmd).Parameters[iIndex]);

   if (conn.State == ConnectionState.Closed)
     conn.Open();

     Oracle.DataAccess.Types.OracleBlob blob = new Oracle.DataAccess.Types.OracleBlob((Oracle.DataAccess.Client.OracleConnection)conn);
     blob = (Oracle.DataAccess.Types.OracleBlob)parm.Value;  //<============== Troubled code

     if (blob.Length > 0)
     {
       byte[] bytes = new byte[blob.Length];
       blob.Read(bytes, 0, (int)blob.Length);
       ReturnValueBytes = bytes;
      }
...


Everything seems to working fine unless the it reaches the line of the code where I am setting the output parameter value to a ORACLEBLOB variable. I understand that the ORACLE BLOB requires the connection to be opened explicitly. Till the troubled line the BLOB connection state is open and as soon as it passes
C#
blob = (Oracle.DataAccess.Types.OracleBlob)parm.Value
the state of the connection inside the BLOB variable is closed.

I am not able to understand why is this happening. Any hints or help would be much appreciated.

Thanks,
Deepak
Posted
Updated 9-Jun-14 5:49am
v2

You should open the connection first, the do everything. Try the below.
C#
using (DbCommand cmd = db.GetStoredProcCommand(sStoredProcName))
{
    cmd.Connection = conn;
    conn.Open();

    // Other codes...
}
 
Share this answer
 
Comments
catchbobbie 10-Jun-14 4:34am    
Thanks Dash,

Have already tried that, If you see I am opening the connection just before declaring the BLOB variable. The problem is the connection gets closed when I assign the out param value to it.
Ok, cool. :)
As it happens most of the time (with me), it was a silly mistake. I was concentrating more on the BLOB variable and almost ignored the rest of the stuff.

I was doing
db.ExecuteNonQuery(cmd);
and the connection was getting closed just after this. I changed my code to
cmd.ExecuteNonQuery();
and its working fine now.

Hope this will help someone else and will save some time.

Cheers,
Deepak
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900