Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3343016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:54:22+00:00 2026-05-18T00:54:22+00:00

I am trying to execute a user-defined Oracle function that returns a RefCursor using

  • 0

I am trying to execute a user-defined Oracle function that returns a RefCursor using ODP.NET. Here is the function:

CREATE OR REPLACE FUNCTION PKG.FUNC_TEST (ID IN TABLE.ID%type)
   RETURN SYS_REFCURSOR
AS
   REF_TEST   SYS_REFCURSOR;
BEGIN
   OPEN REF_TEST FOR
      SELECT   *
        FROM   TABLE;
   RETURN REF_TEST;
END;
/

I can call this function in Toad (select func_test(7) from dual) and get back a CURSOR. But I need to get the cursor using C# and ODP.NET to fill a DataSet, but I keep getting a NullReferenceException – “Object reference not set to an instance of an object”. Here is what I have for that:

OracleConnection oracleCon = new OracleConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
OracleCommand sqlCom = new OracleCommand("select func_test(7) from dual", oracleCon);
sqlCom.Parameters.Add("REF_TEST", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataAdapter dataAdapter = new OracleDataAdapter();
dataAdapter.SelectCommand = sqlCom;

DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);  //FAILS HERE with NullReferenceException

I was able to find lots of info and samples on using stored procedures and ODP.NET, but not so much for returning RefCursors from functions.

EDIT: I do not want to explicitly add input parameters to the OracleCommand object (i.e. sqlCom.Parameters.Add("id", OracleDbType.Int32,ParameterDirection.Input).Value = 7;) as that makes it difficult to implement this as a generic RESTful web service, but I’m reserving it as my last resort but would use stored procedures instead.

Any help is much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:54:23+00:00Added an answer on May 18, 2026 at 12:54 am

    I think you are missing the sqlCom.ExecuteNonQuery();

    also, instead of running the select func_test(7) from dual; lets switch it to run the function and pass in the param

      OracleConnection oracleCon = new OracleConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
    
    
      // Set the command
    
      string anonymous_block = "begin " +
                                  "  :refcursor1 := func_test(7) ;" +
                                  "end;";  
     //fill in your function and variables via the above example
      OracleCommand sqlCom= con.CreateCommand();
      sqlCom.CommandText = anonymous_block;
    
      // Bind 
      sqlCom.Parameters.Add("refcursor1", OracleDbType.RefCursor);
      sqlCom.Parameters[0].Direction = ParameterDirection.ReturnValue;
    
      try 
      {
        // Execute command; Have the parameters populated
        sqlCom.ExecuteNonQuery();
    
        // Create the OracleDataAdapter
        OracleDataAdapter da = new OracleDataAdapter(sqlCom);
    
        // Populate a DataSet with refcursor1.
        DataSet ds = new DataSet();
        da.Fill(ds, "refcursor1", (OracleRefCursor)(sqlCom.Parameters["refcursor1"].Value));
    
        // Print out the field count the REF Cursor
        Console.WriteLine("Field count: " + ds.Tables["refcursor1"].Columns.Count);
      }
      catch (Exception e)
      {
        Console.WriteLine("Error: {0}", e.Message);
      }
      finally
      {
        // Dispose OracleCommand object
        cmd.Dispose();
    
        // Close and Dispose OracleConnection object
        con.Close();
        con.Dispose();}
    

    this is based on the example ODP that can be found @ %ora_home%\Client_1\ODP.NET\samples\RefCursor\Sample5.csproj

    If you want to avoid (for better or worst!) the custom built param collection for each proc/function call you can get around that by utilizing anonymous blocks in your code, I have ammended (once again untested!) the code above to reflect this technique.
    Here is a nice blog (from none other than Mark Williams) showing this technique.
    http://oradim.blogspot.com/2007/04/odpnet-tip-anonymous-plsql-and.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to execute CREATE USER with a given username (via sql parameter) exec
I am trying to run a stored procedure that has multiple in and out
I'm trying to execute the following CFML: <cfquery name=koppelData datasource=#request.DataSource#> INSERT INTO t_user_profile (Username,
Trying to execute this statement in a rails migration to generate a multi column
I have a form that appears as shown in the attached image. I have
Is there any way to turn on driver logging for PHP/MySQL 5? Is there
I've got an unmanaged C++ dll which is being called from a C# app,
To make a long story short... I'm building a web app in which the
I've been doing a bunch of searching and reading today, and haven't figured out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.