Stephen Gray
Stephen Gray

Reputation: 75

Using DataConnectionDialog

When I am attempting to use the DataConnectionDialog from NuGet (version 1.2), I receive the Advanced Settings dialog for setting up the Database Connection. Is there some Setting I have missed or additional library to retreive?

Code:

using System;
using Microsoft.Data.ConnectionUI;

DataConnectionDialog dcd = new DataConnectionDialog();
DataSource.AddStandardDataSources(dcd);
dcd.SelectedDataSource = DataSource.SqlDataSource;
dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
DataConnectionDialog.Show(dcd);

Output:

enter image description here

What I want (this comes from the datasource wizard in Visual Studio Community 2015):

enter image description here

Upvotes: 1

Views: 2279

Answers (1)

Bart Jolling
Bart Jolling

Reputation: 675

I happened to stumble on the same issue. From my main form, I called an async method using Task.Factory.StartNew. This method tries to open the Data Connection Dialog but it would show the Advance Settings dialog box instead.

During troubleshooting, I replaced the DataConnectionDialog with a OpenFileDialog and this gave me a ThreadStateException which pointed me towards the solution.

To solve it, I had to put the code in a separate function, e.g. AskConnectionString and call it using Control.Invoke.

e.g.

public void btnConnString_Click(object sender, EventArgs e)
{
    _connectionString = (string)this.Invoke(AskConnectionString);
}

public string AskConnectionString()
{
    DataConnectionDialog dcd = new DataConnectionDialog();
    DataSource.AddStandardDataSources(dcd);
    dcd.SelectedDataSource = DataSource.SqlDataSource;
    dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
    DataConnectionDialog.Show(dcd);

    return dcd.ConnectionString;
}

Upvotes: 2

Related Questions