farshad Posted August 26, 2003 Posted August 26, 2003 (edited) Hi, in a class module, this is how I am retrieving data. Public Function GetAllCollectionsForParent(ByVal intParentID As Integer) As SqlDataReader Dim strSQL As String = "CSA_SP_GetAllStrategiesParentCollections" Dim oCon As SqlConnection Dim oCmd As SqlCommand Try oCon = New SqlConnection(ConnectToDB) oCmd = New SqlCommand(strSQL, oCon) With oCmd .CommandType = CommandType.StoredProcedure .Parameters.Add("@StrategyParentID", SqlDbType.Int, 4) .Parameters("@StrategyParentID").Value = intParentID oCon.Open() Return .ExecuteReader(CommandBehavior.CloseConnection) End With Catch ex As Exception Throw ex End Try End Function In the form module I then use a datareader to loop through the above records... After about 300 records i receive an error message saying something about the connection pool may have reached its limit. Any thoughts? Thanks Edited August 26, 2003 by divil Quote Farshad
*Gurus* divil Posted August 26, 2003 *Gurus* Posted August 26, 2003 Are you running this method repeatedly? The way you have implemented it could be done better - the code to open (and close, which you don't have) the connection should surround any code that uses the data reader. It is probably trying to create a new connection every time because the existing one is busy with the datareader, maybe you're not closing that either? You should always call .Close() and .Dispose() on objects that support them when you're done with them. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
farshad Posted August 26, 2003 Author Posted August 26, 2003 Hi, Yes, I am running it repeatedly. I can not close it in the above code because then I will not be able to loop through that sqldatareader function Quote Farshad
*Gurus* divil Posted August 26, 2003 *Gurus* Posted August 26, 2003 Which is why you shouldn't be opening it in that function either. The Open and Close of it should really be in the same scope, you could always pass the connection object to that function which could return the datareader from it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
yvesvlb Posted October 3, 2003 Posted October 3, 2003 Heh.. CommandBehaviour.Closeconnection! *sigh* Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.