mysql connection not closing. Too many connections error

I have create a simple .NET windows application which connects to MySQL database to select and insert data. Please see attached code. Iam accessing about four tables in MySQL to load a grid. Upon loading the grid, the connections are not closing and their status is "Sleep" when I check "Show Processlist".

I am calling mysqlconn.close() after every function. Do I need to do anything else. The connections get closed only when the windows application is closed.

This applciation is used in such a way that users open it once and click the "process" button (Click event btnprocessrt_click) several times before closing the application. The event is creating 3-4 connections for every click and the Database is reaching the maximum connections. How can this be fixed?
ee.txt
Angel02Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
I've had some success by also setting the mysql connection object to nothing or null even though the garbage collector should be taking care of it
0
Julian HansenCommented:
Also I would use a different approach. Instead of creating a new connection each time you want to access the database. Open it once and reuse the same connection.

Keep it in a singleton class or similar where you can do a request for the connection which creates the connection first time and then returns that connection on each subsequent request.
0
Angel02Author Commented:
Well, I figured out the unbelievable mistake I made. I was calling Return before closing the connection. And I did that in more than one function..

If CInt(mySQLds.Tables(0).Rows(0).Item(0).ToString()) > 0 Then
                Return True
            Else
                Return False
            End If
            mySQLCmd.Dispose()
            mySQLda.Dispose()
            mySQLds.Dispose()
            MySqlConn.Close()

When I fixed this, the number of connections stopped multiplying.

@ julianH your suggestion does make sense. I am going to use that approach. Thanks!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angel02Author Commented:
Figured out what mistake I made in my code. Details are above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.