Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection UI.Dialog for c# setup project

enter image description here

I add Microsoft.Data.ConnectionUI.Dialog.dll and Microsoft.Data.ConnectionUI.dll dlls to my project, and use this code:

    Microsoft.Data.ConnectionUI.DataConnectionDialog dcd = new Microsoft.Data.ConnectionUI.DataConnectionDialog();

        Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dcd);

        if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dcd) == System.Windows.Forms.DialogResult.OK)
        {
            //
        }
        else
        {
            //
        }

When I use this in normal windows applications, everything seems to be ok (shows standard VS dataconnection dialog with Advanced button). When I use it in SETUP PROJECT, it shows only the advanced dialog AND the OK button is disabled. Users can test the connection but can not press OK button.

Does anyone know what why this is not working?

like image 950
Armen Khachatryan Avatar asked Oct 24 '22 00:10

Armen Khachatryan


2 Answers

The buttons are enabled/disabled depending on the settings.

In this article: http://erikej.blogspot.com.au/2010/04/using-adonet-data-connection-dialog-in.html the author "hardcodes" the datasource and this consequentially disables the "Change" button.

When you set the DataSource name, the OK button should be enabled. I dug up the Data Connection Dialog Source Code code to show you: http://archive.msdn.microsoft.com/Connection/Release/ProjectReleases.aspx?ReleaseId=3863

In the Micrsost.Data.ConnectionUI.Dialog project, open the DataConnectionDialog.cs class and you can see this is the event:

private void ConfigureAcceptButton(object sender, EventArgs e)
{
try
{
acceptButton.Enabled = (ConnectionProperties != null) ? ConnectionProperties.IsComplete : false;
}
catch
{
acceptButton.Enabled = true;
}
}

The above event is hooked up from the ConnectionProperties method and is invoked each time the PropertyChange event fires:

properties.PropertyChanged += new EventHandler(ConfigureAcceptButton);

To get the OK button enabled you will need to satisfy the ConnectionProperties.IsComplete condition.

like image 165
Jeremy Thompson Avatar answered Oct 25 '22 13:10

Jeremy Thompson


I ran into the same problem as you when I ran the DataConnectionDialog from a PowerPoint plugin.

The problem is that the SqlConnectionUIControl must be run in a STA thread. Here is the code I got working.

Load button is clicked, launch the data connector in a thread.

private void dataSourceSelectionButton_Click(object sender, EventArgs e)
{
  Thread browseThread = new Thread(promptForConnectionString);
  browseThread.TrySetApartmentState(ApartmentState.STA);
  browseThread.Start();  
}

Opening the dialog.

public string ConnectionString { get; set; }

private void promptForDataConnection()
{
  DataConnectionDialog dataConnection = new DataConnectionDialog();
  DataConnectionConfiguration connectionConfiguration = new DataConnectionConfiguration(null);
  connectionConfiguration.LoadConfiguration(dataConnection);

  if (DataConnectionDialog.Show(dataConnection) == DialogResult.OK)
  {
    connectionConfiguration.SaveConfiguration(dataConnection);

    this.ConnectionString = dataConnection.ConnectionString;
  }
}

If this doesn't help, picking up the DataConnectionDialog Source might.

like image 42
Mason Avatar answered Oct 25 '22 13:10

Mason