How To Get The Exe Directory For The Current Console Application In C#



TODO:

Have you ever wanted to get the directory for the executing Console Application in C#?  The following method will do so, and is the equivalent of Application.StartupPath when working with Windows Forms.

 

SOLUTION:

 

System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)

 

NOTES:

There are no notes on this topic.

How To Fix SharePoint Error "System.InvalidOperationException: The farm is unavailable."



TODO:

If you have had the error  "System.InvalidOperationException: The farm is unavailable.", and the exception printed below, then try the solution below.

 

Complete Exception:

WebHost failed to process a request.

 Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/33711845

 Exception: System.ServiceModel.ServiceActivationException: The service '/1e1dea56eb8c443782ab6f9948b81e0d/ProfilePropertyService.svc' cannot be activated due to an exception during compilation.  The exception message is: The farm is unavailable.. ---> System.InvalidOperationException: The farm is unavailable.

   at Microsoft.SharePoint.Administration.Claims.SPSecurityTokenServiceManager.get_Local()

 

SOLUTION:

Open IIS

Go to the App Pool, Right click and choose "Advanced"

Set Enable 32-bit Applications = false

 

NOTES:

No notes are available for this topic.

How To Skip The First X Nodes During foreach Loop In XSLT



TODO:

Have you ever wanted to skip the first X nodes during a foreach loop?  In my case, I was taking a flat CSV file and turning it into a generic XML document.  I was then using XSLT to transform that document into a meaningful XML document representing Customer.  The first 2 rows of my CSV file, were header and file information only, therfore I did not want to process them.  Rather than hard code the rows to skip in a pre-processor for the flat file, I can just transform the data to my final document, and then skip X rows in XSLT.  I was using XSLT anyhow to create my customer document, so it was just as easy to put the logic there.

 

SOLUTION:

 

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <Customers>
      <xsl:variable name="myRootNode" select="."/>
      <xsl:for-each select="$myRootNode/MyNodes/Node">
        <xsl:variable name="myNode" select="."/>
        <xsl:if test="position() > 2">
           <Customer>
             Do Your Other XSLT Stuff Here ...
           </Customer>
        </xsl:if>
      </xsl:for-each>
    </Customers>
  </xsl:template>
</xsl:stylesheet>

 

NOTES:

This will skip the first 2 nodes I encounter, which represents the first 2 header rows that I had in my document.

How To List All User Tables In SQL Server



TODO:

Have you ever wanted to list all tables that were user created using T-SQL?

 

SOLUTION:

 

SELECT name FROM SYS.OBJECTS WHERE TYPE='U' order by name

 

 

NOTES:

There are no notes on this topic

How To Get Information On SQL Maintenance Plans Job Steps



TODO:

Have you ever wanted to get information on SQL Server Mainenance Plan job steps using T-SQL?

 

SOLUTION:

select * From msdb..sysjobs

 

 

NOTES:

There are no notes on this topic.

How To Find Text That Exists In A Stored Procedure, Function Or Trigger



TODO:

Have you ever wanted to find text that exists in a stored procedure, function or trigger using T-SQL?

 

SOLUTION:

 

SELECT DISTINCT *
FROM sys.sql_modules m 
INNER JOIN sys.objects  o ON m.object_id=o.object_id
WHERE m.definition Like '%enter your search criteria here%'
ORDER BY 2,1

 

 

NOTES:

There are no notes on this topic.

How To Select The Entire Contents Of A Varchar(max) Or NVarchar(max) Column In SQL Server Management Studio



TODO:

Have you ever wanted to select a varchar(max), or nvarchar(max) column in SSMS?  You will notice that the data is truncated when you have a large amount of data stored in the column.  To get around this we will convert the string to XML, then save the results to an XML file, so you can open in your favorite XML editor / viewer.

 

SOLUTION:

 

--to select a column with NON-XML data
select convert(xml,'<xml><![CDATA[' + cast(LogData as varchar(max)) + ']]></xml>') FROM LogTable where Id = 1234

--to select a column with XML data
select convert(xml, LogData) FROM LogTable where Id = 1234

 

 

NOTES:

The first example wraps the data in CDATA tags, so that it will be well-formed XML. 

The second example does not, because I am storing well-formed XML in the column already.

How To Encrypt / Protect Web.config Or App.config File



TODO:

Have you ever wanted to protect your web.config file, so that the appsettings and connectionstrings sections could not be read?  Below are 2 methods and a class level variable you can add to your code to protect your app.config or web.config.

 

SOLUTION:

 

private string LastErrorMessage = "";
        
/// <summary>
/// Protect the App.Config
/// </summary>
public void ProtectApplicationConfig()
{
    Configuration standardConfig = null;
    ConnectionStringsSection standardConnectionStringsSection = null;
    AppSettingsSection standardAppSettingsSection = null;

    try
    {
        standardConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        // Get the connection strings section
        standardConnectionStringsSection = standardConfig.ConnectionStrings;
        standardAppSettingsSection = standardConfig.AppSettings;

        // Protect this section by using the DpapiProtectedConfigurationProvider a.k.a. DataProtectionConfigurationProvider
        if (!standardConnectionStringsSection.SectionInformation.IsProtected)
            standardConnectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

        // Protect this section by using the DpapiProtectedConfigurationProvider a.k.a. DataProtectionConfigurationProvider
        if (!standardAppSettingsSection.SectionInformation.IsProtected)
            standardAppSettingsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

        // Save the configuration section within protected data
        standardConnectionStringsSection.SectionInformation.ForceSave = true;
        standardAppSettingsSection.SectionInformation.ForceSave = true;

        standardConfig.Save();
    }
    catch (Exception x)
    {
        LastErrorMessage = x.Message;
    }
    finally
    {
        standardConfig = null;
        standardConnectionStringsSection = null;
        standardAppSettingsSection = null;
    }
}

/// <summary>
/// Protect the Web.Config
/// </summary>
public void ProtectWebConfig()
{
    Configuration standardConfig = null;
    ConnectionStringsSection standardConnectionStringsSection = null;
    AppSettingsSection standardAppSettingsSection = null;

    try
    {
        standardConfig = WebConfigurationManager.OpenWebConfiguration("~");

        // Get the connection strings section
        standardConnectionStringsSection = (ConnectionStringsSection)standardConfig.GetSection("connectionStrings");
        standardAppSettingsSection = (AppSettingsSection)standardConfig.GetSection("appSettings");

        // Protect this section by using the DpapiProtectedConfigurationProvider a.k.a. DataProtectionConfigurationProvider
        if (!standardConnectionStringsSection.SectionInformation.IsProtected)
            standardConnectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

        // Protect this section by using the DpapiProtectedConfigurationProvider a.k.a. DataProtectionConfigurationProvider
        if (!standardAppSettingsSection.SectionInformation.IsProtected)
            standardAppSettingsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

        // Save the configuration section within protected data
        standardConnectionStringsSection.SectionInformation.ForceSave = true;
        standardAppSettingsSection.SectionInformation.ForceSave = true;

        standardConfig.Save();
    }
    catch (Exception x)
    {
        LastErrorMessage = x.Message;
    }
    finally
    {
        standardConfig = null;
        standardConnectionStringsSection = null;
        standardAppSettingsSection = null;
    }
}

 

 

NOTES:

The config file can ONLY be used on the machine it is protected on.  Once a file is protected, trying to copy the protected file to another machine and use it will not work.

How To Script All Indexes Of A Table Using SQL Server



TODO:

Have you ever wanted to script all indexes for a particular table in SQL Server?

 

SOLUTION:

The solution is simple and requires no code.

  1. Open SQL Server Management Studio
  2. Click "Tools"
  3. Click "Options"
  4. Expand "SQL Server Object Explorer"
  5. Click "Scripting"
  6. Scroll to "Table And View Options"
  7. Click "Script Indexes" and set it to "True"
  8. Now right click on your Table in Object  Explorer
  9. Now Click "Script Table As" -> "CREATE TO" => "New Query Editor Window"
  10. All indexes for the table will be in the script generated.

 

NOTES:

There are no notes on this topic.

How To Fix "One Or More Activex Controls Could Not Be Displayed Because..." When You Open Services



TODO:

All of a sudden, you get a message "One Or More Activex Controls Could Not Be Displayed Because..." when you open Services.

 

SOLUTION:

Go to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ and delete the key before "0".

 

NOTES:

I think you can thank Adobe Updater for this one. I noticed I had an Update that I declined supposedly forever since I checked "do not remind me again",

and since that boot, I had the error.