Christian Verner
Christian Verner

Reputation: 113

c# visual studio using settings.settings for mysql connection

I'm trying to connect to a MySQL database using settings.settings in visual studio 2017. I checked settings.settings and my connection string is there and is set properly (as a connection string and is complete). I looked everywhere and tried a bunch of different code to get it to work but it's simply not working. I've been at this for way too many hours so your help would be greatly appreciated. If you could post the code you would use it would be great. I am using the MySql.Data.MySqlClient and System.Configuration. I can connect by inserting the connection string manually but I really want to use the settings since I will need to change this info once the system is installed on location and don’t want to change the code on all the forms once there.

Here is my code:

public dbadmin()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
            MySqlConnection con = new MySqlConnection(conSettings.ToString());
            con.Open();
            MessageBox.Show("Connection ok");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }




    }

Upvotes: 0

Views: 248

Answers (1)

pfx
pfx

Reputation: 23214

The connectionstring requested via ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"] gets retrieved from the App.configfile instead of Settings.settings.

If you want to keep using ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"] you have to add the connectionstring to App.config as shown below.
(Provide your values for connectionStringand providerName.)

<configuration>
    <connectionStrings>
        <add name="shopmanagerConnectionString1" connectionString="..." providerName="..." />
    </connectionStrings>
</configuration>

Upvotes: 1

Related Questions