Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question:
When i am trying to open the connection of oracle database in C#, then the following error occurred. Mention that this code work on new project. What is the solution for this.

Quote:
ArgumentNullException:

Value cannot be null.
Parameter name: SafeHandle cannot be null.


Stack Trace:

at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at System.Data.Common.UnsafeNativeMethods.OCIAttrSet(OciHandle trgthndlp, HTYPE trghndltyp, Byte[] attributep, UInt32 size, ATTR attrtype, OciHandle errhp)
at System.Data.OracleClient.TracedNativeMethods.OCIAttrSet(OciHandle trgthndlp, Byte[] attributep, UInt32 size, ATTR attrtype, OciHandle errhp)
at System.Data.OracleClient.OciHandle.SetAttribute(ATTR attribute, String value, OciErrorHandle errorHandle)
at System.Data.OracleClient.OracleInternalConnection.OpenOnLocalTransaction(String userName, String password, String serverName, Boolean integratedSecurity, Boolean unicode, Boolean omitOracleConnectionName)
at System.Data.OracleClient.OracleInternalConnection..ctor(OracleConnectionString connectionOptions)
at System.Data.OracleClient.OracleConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OracleClient.OracleConnection.Open()
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at FlexiStar.DataProvider.OracleProcedureProvider.GetSpParameterSet(String connectionString, String spName, Boolean includeReturnValueParameter, String& reply) in C:\Users\shihabiu\Desktop\Middleware.Net\Source\QCash Middleware 2.0.2\FlexiStar.DataProvider\MSOracleDataprovider\OracleProcedureProvider.cs:line 294


Code
C#
String cs = @"Data Source=loacalhost/orcl;Integrated Security=no;User ID=fspro;Password=fspro";
using (OracleConnection con = new OracleConnection(cs))
{
    OracleDataAdapter da = new OracleDataAdapter("FSO_CFG_SYSTEM_GA", con);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
   da.SelectCommand.Parameters.Add("P_RESULT_SET_OUT", OracleType.Cursor).Direction = ParameterDirection.Output;

    DataTable dt = new DataTable();
    try
    {
        da.Fill(dt);
    }
    catch (OracleException e)
    {
        throw new Exception(e.Message, e.InnerException);
    }
    catch (Exception e)
    {
        throw new Exception(e.Message, e.InnerException);
    }
    return dt;
}
Posted
Updated 4-Mar-15 0:02am
v4
Comments
CHill60 3-Mar-15 7:35am    
We can't see your screen, access your HDD nor read your mind. We need to see the code that is causing the exception
phil.o 3-Mar-15 7:39am    
The only thing we can say is that the problem is at FlexiStar.DataProvider.OracleProcedureProvider.GetSpParameterSet(String connectionString, String spName, Boolean includeReturnValueParameter, String& reply) in C:\Users\shihabiu\Desktop\Middleware.Net\Source\QCash Middleware 2.0.2\FlexiStar.DataProvider\MSOracleDataprovider\OracleProcedureProvider.cs:line 294
HKHerron 3-Mar-15 16:02pm    
Show us your code that attempts to open the connection!
Mohsen Heydari 3-Mar-15 17:22pm    
It seams a problem with oracle client installation. Can you connect to Oracle with other applications like plsql or toad?
Herman<T>.Instance 5-Mar-15 11:25am    
any parameter added that has a NULL value will not be send as parameter to the database. That is why Oracle tells you: SafeHandle cannot be null.
Use DBNull.Value in stead of NULL

Example:
da.SelectCommand.Parameters.Add("P_RESULT_SET_OUT", OracleType.Cursor ?? DBNull.Value).Direction = ParameterDirection.Output;

1 solution

any parameter added that has a NULL value will not be send as parameter to the database. That is why Oracle tells you: SafeHandle cannot be null.
Use DBNull.Value in stead of NULL

Example:
SQL
da.SelectCommand.Parameters.Add("P_RESULT_SET_OUT", OracleType.Cursor ?? DBNull.Value).Direction = ParameterDirection.Output;
 
Share this answer
 
v2

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