Search code examples
asp.netoracle-databasegridviewsys-refcursor

Binding ASP.NET GridView to an Oracle SYS_REFCURSOR


We have a Procedure in Oracle with a SYS_REFCURSOR output parameter that returns the data we want to bind to an ASP.NET GridView control. I've seen this done before but I can't find the original reference I used to solve the problem.

Here is what the procedure looks like:

create or replace PROCEDURE GETSOMEDATA
(
    P_Data OUT SYS_REFCURSOR
)
AS
BEGIN
    OPEN P_Data FOR SELECT * FROM SOMETABLE;
END GETSOMEDATA;

And for now the GridView is just bare-bones:

<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="true"></asp:GridView>

Solution

  • Try something like: (didn't specify which language)

        Public Function GetSomeData() as DataTable
            Dim OrclConn as New OracleConnection("Connectionstring")
            Dim OrclCmd as New Oraclecommand("GETSOMEDATA", OrclConn)
            OrclCmd.CommandType = CommandType.StoredProcedure
            OrclCmd.Parameters.Add("P_Data", OracleType.Cursor).Direction = ParameterDirection.Output 'Or ParameterDirection.ReturnValue
    
            Dim OrclDA as New OracleDataAdapter(OrclCmd)
            Dim RtnTable as DataTable
            OrclConn.Open
            OrclDA.Fill(RtnTable)
            OrclConn.Close
    
            Return RtnTable
        End Function