Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Custom action for checking sqlserver connection not validating correctly. Test connection button click custom action as below .Let me know how i can rectify it and make it working correctly

C#
public static ActionResult TestSqlConnection(Session session)
       {
           try
           {
               if (session == null)
               {
                   throw new ArgumentNullException("session");
               }

               SetSessionProperty(session, "DATABASE_TEST_CONNECTION", "0");
               string sConnectionString = GetConnectionString(session, false);
               if(string.IsNullOrEmpty(sConnectionString))
               {
                   result = ActionResult.Failure;
               }
               else
               {
                   using (SqlConnection sqlConect = new SqlConnection(sConnectionString))
                   {
                       sqlConect.Open();
                       SetSessionProperty(session, "DATABASE_TEST_CONNECTION", "1");
                       MessageBox.Show("Test Connection Successful", "Test connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       result = ActionResult.Success;
                   }
               }
           }
           catch (Exception ex)
           {
               InstallUtilities.WriteLogInstall(session, "exception occurs while connecting to database server", ex, true);
               MessageBox.Show(ex.Message, "Test Coonection", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
           return result;
       }

private static string GetConnectionString(Session session, bool isCustomActionData)
       {
           string sConnectionString=string.Empty;
           if (GetSessionProperty(session, "DATABASE_SERVERNAME",isCustomActionData)=="0")
           {
               sConnectionString = string.Empty;
           }
           else
           {
               if((GetSessionProperty(session,"DATABASE_SERVERNAME",isCustomActionData)=="1"))
               {
                   if ((GetSessionProperty(session, "DATABASE_WINDOWSAUTHENTICATION", isCustomActionData) == "1"))
                   {
                       sConnectionString = string.Format("Integrated Security=SSPI;Persist Security Info=False;Data Source={0};",
                           GetSessionProperty(session, "DATABASE_SERVERNAME", isCustomActionData).Trim());
                   }
                   else
                   {
                       sConnectionString = string.Format("Persist Security Info=False;Data Source={0};User ID={1};Password={2};",
                           GetSessionProperty(session, "DATABASE_SERVERNAME", isCustomActionData),
                           GetSessionProperty(session, "DATABASE_USERNAME", isCustomActionData),
                           GetSessionProperty(session, "DATABASE_PASSWORD", isCustomActionData));
                   }
               }
           }
           return sConnectionString;
       }


this is my database setting Dialog structure
XML
<Control Id="Test" Type="PushButton" X="25" Y="243" Width="100" Height="17" Text="!(loc.DatabaseServerDlg_Test)">

  <Condition Action="disable"><![CDATA[DATABASE_WINDOWSAUTHENTICATION = "0" AND DATABASE_USERNAME = ""</Condition>
  <Condition Action="enable"><![CDATA[DATABASE_WINDOWSAUTHENTICATION = "1" OR (DATABASE_WINDOWSAUTHENTICATION = "0" AND DATABASE_USERNAME <> "" AND DATABASE_PASSWORD <> "")</Condition>

  <Publish Event="DoAction" Value="CA_DataBaseTestConnection" Order="1">1</Publish>
  <Publish Property="DATABASE_VALID_LOGON" Value="1" Order="2"><![CDATA[DATABASE_TEST_CONNECTION = "1"</Publish>
  <Publish Property="DATABASE_VALID_LOGON" Value="0" Order="2"><![CDATA[DATABASE_TEST_CONNECTION = "0"</Publish>

  <Publish Property="ERROR_MESSAGE" Value="N" Order="3"><![CDATA[(DATABASE_TEST_CONNECTION = "1")</Publish>
  <Publish Property="ERROR_MESSAGE" Value="!(loc.DatabaseServerDlg_AuthenticationErrorMessage)" Order="3"><![CDATA[DATABASE_TEST_CONNECTION = "0"</Publish>
  <Publish Event="SpawnDialog" Value="GenericErrorDlg" Order="4"><![CDATA[DATABASE_TEST_CONNECTION = "0"</Publish>
</Control>

<Control Id="TestResult_Success" Type="Bitmap" X="250" Y="10" Width="24" Height="24" Text="BIN_SuccessBmp">
  <Condition Action="hide"><![CDATA[DATABASE_VALID_LOGON <> 1</Condition>
  <Condition Action="show"><![CDATA[DATABASE_VALID_LOGON = 1</Condition>
</Control>
<Control Id="TestResult_Failure" Type="Bitmap" X="250" Y="10" Width="24" Height="24" Text="BIN_ErrorBmp">
  <Condition Action="hide"><![CDATA[DATABASE_VALID_LOGON = 1 </Condition>
  <Condition Action="show"><![CDATA[DATABASE_VALID_LOGON <> 1</Condition>
</Control>


this is customaction in customAction.wxs
XML
<CustomAction Id="CA_DataBaseTestConnection" BinaryKey="BIN_CustomAction" DllEntry="TestSqlConnection" Execute="immediate" Impersonate="yes" Return="ignore" />
Posted

1 solution

I have figured out solution to above issue.
 
Share this answer
 
Comments
CHill60 3-Feb-14 8:56am    
Would you share what the solution was for the benefit of anyone else with similar problems

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900