Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:23:09+00:00 2026-06-12T17:23:09+00:00

I am trying to connect to a mysql database, my credentials(username, password, database name…

  • 0

I am trying to connect to a mysql database, my credentials(username, password, database name… etc) are all correct to my knowledge. I got stuck at coonection.open() statement which displays an error of the connection being already opened.

I searched for help at other sites, it has been reported as a bug… http://support.microsoft.com/kb/823401

While searching for alternative solutions I came across

   using ( connection = MySqlConnection(connectionString))

I was unable to proceed with it. Any help with it would be really appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace HotelManagement
{
class DBConnect
{
    private MySqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;

    //constructor
    public DBConnect()
    {
        Initialize();
    }

    private void Initialize()
    {
        server = "localhost";
        database = "hm";
        uid = "root";
        password = "password";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +    "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);
    }

    public bool openConnection()
    {
        try
        {
            connection.Open();  //*SHOWS ERROR InvalidOperationUnhandled: Conection already open*
            return true;

        }
        catch (MySqlException e)
        {
            //error 0: Cannot Connect to the server
            switch (e.Number)
            {
                case 0: MessageBox.Show("Cannot Connect to the server");
                    break;
                case 1045: MessageBox.Show("invalid username/password");
                    break;
            }
            return false;
        }
    }

    public bool closeConnection()
    {
        try
        {
            connection.Close();
            return true;
        }
        catch (MySqlException e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
    }

    public void insert()
    {
        string query = "insert into test (i) values (10)";
        if (this.openConnection() == true)
        {
            //creating command and connections
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
            this.closeConnection();
        }
    }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:23:10+00:00Added an answer on June 12, 2026 at 5:23 pm

    You must close your connection , you have not closed connection (Maybe after first debug)

    You can use

    connection.Close();
    

    Best Practise :

    I suggest you to use using block, in order to ensure that you clean non managed object in the end of treatment

    using(var connection = new MySqlConnection(...))
    {
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to connect to a remote mysql database from an iPhone. I
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I am trying to connect to a MySQL 5.5 database as follows: public void
I'm trying to write a java program to connect to the same MySQL database
I'm trying to connect to MySQL database from node.js using db-mysql library. Everything works
I am trying to connect to MySQL database with Java and I get the
I am trying to connect to a MySQL database using the mysql_connect() command however
I have been trying to connect to mysql all evening using PHP. I know
I am trying to connect to my mysql database on a remote server (via
I'm trying to connect to a MySQL database using my ASP.NET Web Forms Application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.