Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ADO.NET & Database » Handling Oracle Max Open Cursor Error when Fetching Multiple Rows in ADO.NET

Handling Oracle Max Open Cursor Error when Fetching Multiple Rows in ADO.NET

When working with Microsoft technologies, MS SQL Server comes out as a natural choice for the database. While it is a fact that .NET Framework has extensive support for SQL server, when you work with other databases like Oracle the situation might turn out to be a little more tricky than it is with SQL Server.

Page Views : 33715
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Generally while working with Microsoft technology, MS-SQL server comes out as a natural choice for the database. While it's a fact that .net framework has extensive support for SQL server ,when you work with other databases like oracle the situation might turn out to be a little more tricky than it is with MS-SQL.While fetching records from MS-SQL we are used to simply call the procedure from our .cs /vb file. Even if you are fetching multiple records the same procedure will work as effectively as the one which gets only one row.

But in oracle you have to use something called cursor specifically speaking REF_CURSOR to be able to fetch multiple rows through a stored procedure. And oracle calls it a package which will have a specification and a package body.

The oracle package to fetching multiple record will look like this:

CREATE OR REPLACE PACKAGE PKG_Candidates_List as
TYPE Select_List_RefCur is REF CURSOR;
PROCEDURE Get_Candidates_Passed (Candidates_ListCur OUT Projects_List_RefCur);
End;
/
CREATE OR REPLACE PACKAGE BODY PKG_Candidates_List as
Procedure
Get_Candidates_Passed (Candidates_ListCur OUT Projects_List_RefCur)
IS
BEGIN
open
Candidates_ListCur for
SELECT
A.Candidate_ID, A.Candidate_Name, A.Candidate_Marks, B.Pass_Date, B.Pass_Status
FROM Candidate A, Marks B
Where A.Candidate_ID = B.Candidate_ID AND A.Pass_Date = B.Pass_date
AND B.Pass_Status = 'Passed';
End;
End;
/

If you are using a DAL for accessing your database then the procedure call in your cs page would look something like this:-

OracleParameter ps = new OracleParameter("contactCur",OracleType.Cursor);
ps.Direction = ParameterDirection.Output;
OracleParameter param_sowID =
new OracleParameter("P_NM_SOWID",OracleType.Number);
param_sowID.Direction = ParameterDirection.Input;
param_sowID.Value = Request.QueryString["ID"].ToString();
OracleParameter[] param = {ps,param_sowID};
DataSet ds = -OracleHelper.ExecuteDataset(connectString,CommandType.StoredProcedure, "PKG_GET_CONTACTINFO.GetReadOnlyData",param);
//

Now this code works fine almost always till you exceed the maximum no of open cursor. That's when you get something like this:

ORA-01000: maximum open cursors exceeded

Though you have opened the cursor and are able to fetch the record on the fly ,there is a slight problem here. You will find that when the database calls increase in number you will get an error. To all those working with SQL server for years the error will seem funny to say the least. There is no problem with the code per se, but oracle has a severe limitation when working with MS.Net. You will notice in the above mentioned oracle package that we open a cursor which returns multiple rows but you are nowhere closing it and as the database calls increase oracle reaches its limit and your code bursts. As of now ,there is no way you can close the cursor from your .cs or .vb page.

At this point an easy way out would be to increase the maximum no of open cursor the oracle server permits. You can do this by going to the oracle command prompt and typing

ALTER SYSTEM SET open_cursors=1500 scope=both;

Alternatively you can go to the init.ora ( urdatabase\admin\cwld\pfile\init.ora ) file and change the parameter to a desired number .Though the maximum no of open cursor could be only 2000 ,still in many situation I guess it would help.

Remember, these changes will not take effect until you restart your database/machine ,and the maximum no of cursor allowed by oracle will also be dependent on the server m/c configuration.

However if you still get the same problem that is because your application is opening more no. of cursor than your oracle server allows .This happens because each database call opens a cursor which is not getting closed and you have no direct way of closing it. So the only way you can close the cursor is by destroying the command object which opens the cursor. In other words disposing your command object by simply calling cmd.dispose()everywhere its getting used. Something like the following:

//--
public static DataSet ExecuteDataset(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =
new OracleCommand();
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
OracleDataAdapter da =
new OracleDataAdapter(cmd);
DataSet ds =
new DataSet();
da.Fill(ds);
cmd.Dispose();
return ds;
}
//--

This way you control the maximum number of open cursors from your c# or vb.net pages and avoid getting errors when fetching multiple rows from oracle.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
vivek.shaurya
Vivek Shaurya am working as a Technical Lead with Wipro Technologies.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.