Reputation: 75
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:
What I want (this comes from the datasource wizard in Visual Studio Community 2015):
Upvotes: 1
Views: 2279
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