none
Type cast issue 'System.Data.OracleClient.OracleCommand' to 'Oracle.DataAccess.Client.OracleCommand' RRS feed

  • Question

  • We are using ODP.NET oracle provider to connect to oracle DB using Enterprise library 5.0.

    The below code works fine:

    ===============================================================
     Database database = DatabaseFactory.CreateDatabase(ORACLE_CONNECTION_CONFIG_NAME);
     using (OracleCommand dbCommand = (OracleCommand)database.GetStoredProcCommand(SP_NAME))
      {
        dbCommand.Parameters.Add("parameters");
        databaee.ExecuteReader(dbCommand);
      }

    ========================================================================

     

    But as per our requirement we need to decrypt the connection string using a custom logic before creating the database connection, so we cannot use DatabaseFactory.CreateDatabase() method.

     

    So we are using the below code:

    =================================================================================

    Database databsebase= new  Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase("encrypted connection string");
    using (OracleCommand dbCommand = (OracleCommand)database.GetStoredProcCommand(SP_NAME))  

    {
    dbCommand.Parameters.Add("parameters");
    databaee.ExecuteReader(dbCommand);
    }

      

    But at runtime, above Oracel command type casting is throwing the following error:

    Unable to cast object of type 'System.Data.OracleClient.OracleCommand' to type 'Oracle.DataAccess.Client.OracleCommand'.

    Can somebody tell me why this would throw error

     

    • Edited by Sanban Wednesday, July 20, 2011 7:07 PM edit
    Wednesday, July 20, 2011 7:04 PM

Answers

  • I resolved this, here is the code:

          DbProviderFactory providerFactory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
          Database dbServer = new Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase("connection string", providerFactory);

    • Proposed as answer by LiluPat Monday, July 25, 2011 6:20 AM
    • Marked as answer by Larcolais Gong Thursday, August 4, 2011 6:54 AM
    Monday, July 25, 2011 6:20 AM

All replies

  • Do you have System.Data.OracleClient referenced in your project? If so, you shouldn't need it if you are using ODP.NET.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, July 20, 2011 7:26 PM
  • i think you can check this link was similar with yours. http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=9007 Additionally, it looks that your question was related to Oracle. i think you may get more suggestons there. Click here.


    Just a newbie for everything.
    Saturday, July 23, 2011 2:09 PM
  • I resolved this, here is the code:

          DbProviderFactory providerFactory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
          Database dbServer = new Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase("connection string", providerFactory);

    • Proposed as answer by LiluPat Monday, July 25, 2011 6:20 AM
    • Marked as answer by Larcolais Gong Thursday, August 4, 2011 6:54 AM
    Monday, July 25, 2011 6:20 AM
  • Hi,

    It helps me also to solve similar problem.

    Are you able to call storedprocedure with parameters by passing them as generic array?

    It throws me error as "Parameter discovery is not supported for connections using GenericDatabase. You must specify the parameters explicitly, or configure the connection to use a type deriving from Database that supports parameter discovery."
    • Edited by suryahon Monday, June 11, 2018 9:28 AM Error text was missing
    Monday, June 11, 2018 9:26 AM
  • Hi, Is there any resolution to the problem. Similar to suryahon post I am too facing the same issue.
    Sunday, September 30, 2018 4:13 PM