0

below is my connection. Can anyone help look on this please. Got the error when tried to launch the windows form application.

The error is:

System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;

namespace Csharp_Simple_Hotel_System
{
    /*
     *  in this class will make the connection between our app and mysql database
     *  first you need to download the mysql connector and add it to your project
     *  download link - >
     */
    class CONNECT
    {
        private MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=csharp_hotel_db");

        // create a function to return our connection
        public MySqlConnection getConnection()
        {
            return connection;
        }

        //create a function to open the connection
        public void openConnection()
        {
            if(connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        //create a function to close the connection
        public void closeConnection()
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }
    }
}

Let me know if you need further information for me.

Thank you.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. – Teoh Che Leong Mar 22 '21 at 10:41
  • File name: 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at MySql.Data.MySqlClient.MySqlConnectAttrs.InitOSDetails() at MySql.Data.MySqlClient.MySqlConnectAttrs..cctor() – Teoh Che Leong Mar 22 '21 at 10:42
  • Looks like no actual code problem but the referenced assemblies (libraries) of your project. – Cleptus Mar 22 '21 at 11:30
  • Just by the way, the code you have is not the correct way to go about databsae connections. Create connections where you need them, and dispose immediately with `using`. Do NOT cache the connection. See https://stackoverflow.com/questions/17552829/c-sharp-data-connections-best-practice – Charlieface Mar 22 '21 at 12:02

1 Answers1

2

Found a solutions. Instead of install mysql connector windows package, we can install mysql data from nuGET package from Microsoft Visual Studio.

Can go to Tools > nuGET Package Manager > manage nuGet Package for solutions > browse for Mysql connector and install it.

BOOM! Problem SOLVED!

  • It runs, but working with databse connections like you have done is problematic. Check Charlieface comment's link in your question. – Cleptus Mar 23 '21 at 09:00