Category Archives: C#

IIS Express – Disabling anonymous authentication

StackOverflow: IIS Express is automatically disabling anonymous authentication for my project, why?

To disable anonymous authentication, I found updating the following element in the project file (i.e. csproj, vbproj) allowed me to debug my application with anonymous access.

<IISExpressAnonymousAuthentication>disabled</IISExpressAnonymousAuthentication>

Changing the IISExpressAnonymousAuthentication setting to enabled fixes the issue (it can also be done within Visual Studio, select project, F4, set Anonymous Authentication in the properties grid to Enabled).

String.Format to set a constant in .NET

Today I began working on the base framework for a new project. In the past we would just copy an existing project and remove what we don’t need, so I thought this would be a good time to make the framework more generic to speed up this part of the process for future projects. This also would prevent having to copy and replace for constants, variables, functions, class names, etc. based on the name of the project as we’ve done in the past. I say all of this to point out something I found that was new to me.

There were constants in the framework that needed to be changed, for example:

Private Const cUserName As String = "ProjectUserName"

Where “Project” in “ProjectUserName” would need to be changed based on the name of the project, this happen for several other constants. So I tried setting the constant like this:

Private Const cName As String = String.Format("{0}UserName", AppSetting.ProjectID)

And got “Constant expression is required.” which wasn’t clear to me. After some research I found that a constant has to be set by a compile time constant and the result of String.Format isn’t a compile time constant.

I finally just changed the constant to a shared readonly variable.

Private Shared ReadOnly cUserStoreName As String = String.Format("{0}UserName", AppSetting.ProjectID)

SQL Server Reporting Services (SSRS) – Download Multiple RDL files

ssrs dashboardI needed to migrate multiple SSRS reports (.rdl’s) between environments (production -> qa ->development) and didn’t want to download/upload each individual report. I searched but couldn’t find a solution built into SSRS report manager, so I began my Google search. I found a solution on Code Project (SSRS Downloading .RDL Files) that worked after some tweaking.

This project written in C# using Visual Studio 2010, and all I really needed to change was the URL to the SSRS web service to get the project up and running. Some of the other changes I made included building the folder structure as it is on the SSRS server and only saving the .rdl files.

Here’s what I came up so far:
SSRSExtractor.zip

After I ran the project and downloaded all the reports really fast, I still had to manually upload the ones I needed to migrate. When I have time hopefully I can implement uploading to an SSRS server into the project.

Collection Types Commonly Used in .NET

This is a question that was asked during an interview and I didn’t know the correct answer, so I thought I would add the information I found to “My Online Notepad“. Maybe it can also help someone else.

  • Hash Tables – A collection of key/value pairs that are organized based on the hash code of the key. The Dictionary(Of TKey, TValue) and ConcurrentDictionary(Of TKey, TValue) classes have similar functionality as the Hashtable. The elements of Hashtable are of type Object, so boxing and unboxing typically occur when you store or retrieve a value type.
  • Queues – A first-in, first-out collection of objects. This is useful if you want to access the information in the same order that it is stored in the collection. There are three primary operation that can be performed on Queues and its element: Enqueue, Dequeue, Peek
  • Stacks – A simple last-in-first-out (LIFO) non-generic collection of objects. There are three primary operation that can be performed on Stacks and its element: Push, Pop, Peek
  • Dictionaries – A collection of keys and values. Typically provides better performance than a Hashtable for value types.
  • Lists – A strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

XML documentation parse error (warning in Visual Studio)

I was working with a VB.NET project today and noticed the following warning:

XML documentation parse error: Whitespace is not allowed at this location. XML comment will be ignored.

After a quick search on the web I found the problem was using the ‘&’ character which is a reserved character for XML documentation. So my solution was to change ‘&’ to ‘and’ but I also found that placing the comment inside of <![CDATA[ Comments ]]> works. Here’s an example of my comment:

''' 
''' This is a comment with an ampersand & causes a warning in Visual Studio.
''' 
''' 
''' 
''' 
Public Property s1() As String
	Get
		Return ""
	End Get
End Property

.NET ToString Format Specifiers

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):. . . . . . . . -123 (default = ‘G’)
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = ‘G’)
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = ‘G’)
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

Ways to validate Phone Numbers in C#

using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {
      string text = "124";

      if ( !Regex.Match(text,@"^[1-9]\d{2}-[1-9]\d{2}-\d{4}$" ).Success )
      {
         Console.WriteLine( "Invalid phone number");
      }   
   }
}

Validate Phone Number

public bool IsValidUSPhone(string number)
{
    return Regex.IsMatch(number, @"\(\d{3}\)\s\d{3}-\d{4}");
}

Matching and Grouping Regular Expressions using Regex in C#

Inline IF’ Statement

C#

string s; 
s = (true ? "True" : "False"); //s will be "True"

VB.NET

Dim s As String
s = If(True, "True", "False") 's will be "True"
'In the following you cannot rely on a particular function 
'not being called if the other argument is selected by Expression
'(notice IIf vs. If)
s = IIf(True, "True", "False") 's will be "True"

JavaScript

var s = (true) ? 'True' : 'False'; //s will be "True"