Skip to content

Instantly share code, notes, and snippets.

@yvanin
Last active November 8, 2019 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yvanin/ab40548fc6bf7a1d8a68 to your computer and use it in GitHub Desktop.
Save yvanin/ab40548fc6bf7a1d8a68 to your computer and use it in GitHub Desktop.
Test SQL connection in Wix

Custom action (in C# dll):

[CustomAction]
public static ActionResult TestDbConnection(Session session)
{
    using (var connection = new SqlConnection(session["CONNECTION_STRING"]))
    {
        try
        {
            connection.Open();
            session["TEST_DB_CONNECTION_RESULT"] = "Connection succeeded.";
        }
        catch
        {
            session["TEST_DB_CONNECTION_RESULT"] = "Connection failed!";
        }
        return ActionResult.Success;
    }
}

Popup that will display the test result message (in .wxs file):

<Fragment>
    <UI>
        <Dialog Id="TestDbConnectionResultDlg" Width="200" Height="80" Title="Testing DB Connection...">
            <Control Id="OK" Type="PushButton" X="75" Y="50" Width="56" Height="17" Default="yes" Cancel="yes" Text="OK">
                <Publish Event="EndDialog" Value="Return">1</Publish>
            </Control>
            <Control Id="Text" Type="Text" X="20" Y="12" Width="160" Height="40" Text="[TEST_DB_CONNECTION_RESULT]" />
        </Dialog>
    </UI>
</Fragment>

Button that will trigger sql connection test (in .wxs file):

<Control Id="TestDbConnection" Type="PushButton" X="20" Y="100" Height="16" Width="45" Text="Test">
    <Publish Event="SpawnDialog" Value="TestDbConnectionResultDlg" Order="2">1</Publish>
    <Publish Event="DoAction" Value="TestDbConnection" Order="1">1</Publish>
</Control>
@doubleII
Copy link

doubleII commented Jul 15, 2018

Where should I initialize the CONNECTION_STRING? I have a InvalidOperationException, if I execute the code.
CONNECTION_STRING has not been initialized exception.

Thanks for the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment