C#: Regular Expression URL and Format HTML

When developing an application it is often a requirement to identify and format a URL within text. One example would be in the formatting of user input that is saved and displayed on a web page. The following code uses a Regular Expression to identify links within text and replace the links with HTML creating a valid HyperLink when displayed in a browser.
 
public static string FormatEntry( string e )
        {
            string original = e;
            try
            {
                string pattern = @"((ht|f)tp(s?):\/\/([\w.]+\/?)\S*)";
                Regex rgx = new Regex ( pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled );
                e = rgx.Replace ( e, "<a href=\"$1\" target=\"_blank\">$1</a>" );
                return e;
            }
            catch
            {
                return original;
            }
        }
RegEx URL

C#: Set ServiceName using an Installation Parameter

Often it is necessary to set the ServiceName for a Windows service at the time of installation. This is helpful when there will be more than once installation of the same server or if a installation specific name is needed. One way to set the ServiceName is to pass a parameter during installation that is read by the Installer Class:

 

private void BeforeInstallEventHandler( object sender, InstallEventArgs e )
        {
            // Add steps to perform any actions before the install process.
            string name = Context.Parameters["ServiceName"];
            if ( name != null )
            {
                serviceInstaller.ServiceName = name;

            }
            else
            {
                serviceInstaller.ServiceName = "YourServiceName";
            }

        }

 

The InstallContext.Parameters Property is used to obtain the parameters that were used when the installutil.exe was run. In the above example (don't forget your exception handling) if the parameter "ServiceName" is used then the service is installed with the name specified. If it is not specified then the "default" servicename is used.

 

The installation of the service could be executed with the following:

SQL: Determine Disk Space usage for Tables

The stored procedure sp_spaceused can be used to determine the reserved and used disk space for a database or specific objects (tables or indexed views) within a database. If sp_spaceused is called without an @objectname (table or indexed view) the results are displayed for the database. If an @objectname is specified then the results are displayed for the object specified.

EXEC sp_spaceused

EXEC sp_spaceused

 

EXEC sp_spaceused '@TableName'

EXEC sp_spaceused

 

To list information for all of the tables within a database the undocumented procedure sp_MSforeachtable can be used:

EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

 

SQL: Include Row Number in SELECT Query Results

When selecting a recordset from a SQL database, it is often necessary to include a unique row number for each record. The NEWID() function can be included in the query to include a unique GUID for each record. This may satisfy some scenarios however, in the case where a numeric row number is needed the ROW_NUMBER() function can fulfill the need. The ROW_NUMBER() function returns a sequential row number of the records in the result.
SELECT
	ROW_NUMBER()OVER (ORDER BY [Name]) AS Row
	,[Name]
	,[Address]
	,[City]
	,[State]
	,[Post Code]
	,[Country]
FROM
	[Customers]
WHERE
	([Country] in ('CA','US'))
ROW_NUMBER()

ASP.NET: Programmatically set the InnerHtml of a <div>

To programmatically set the InnerHtml of a <div> control in a web form set the <div> control to runat = ”server”:

<div id="myDIV" runat="server">

With the <div> set to run on the server it is accessible via the codebehind page:

myDIV.InnerHtml = "<font color='red'>Message</font>";

Reading Stuff

Information in this document subject to change without notice.
All Software source code published is for demonstration and knowledge sharing purposes only. The Code is supplied "as is" without warranty as to result, performance or merchantability. Use at your own risk.
The opinions expressed herein are the opinions of the author and do not reflect those of any other entity.