Sunday, February 13, 2011

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>
  • just GooglingIt trying to find out an answer for you, I come across this article.

    maybe it can help you on this matter.

    From balexandre
  • 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
    
    jamminjulia : This is the correct solution. To bind this data to the GridView, just set the GridView's DataSource = RtnTable and call DataBind().
  • Dim oracon As New OracleConnection("User Id=developer;Password=developer;Data Source=orcl;") Dim ds As New Data.DataSet Dim qry As String oracon.Open() qry = "select * from Employee" Dim adp As New OracleDataAdapter(qry, oracon) adp.Fill(ds) GridView1.DataSource = ds GridView1.DataBind() oracon.Close()

0 comments:

Post a Comment