How to use a ValidationSummary to display server errors?

Posted January 13th, 2010 in .NET, ASP.NET, Tips and Tricks by Sam Beauvois

Imagine you want to use your ValidationSummary to display server errors as client error, there is a simple way to do that.

First, declare your ValidationSummary, and a CustomValidator with Display property set to “None”


<asp:CustomValidator ID="ServerErrorCustomValidator"
Display="None" runat="server" ValidationGroup="MyValidationGroup" />

<asp:ValidationSummary ID="AdministrationTaskValidationSummary" runat="server" ValidationGroup="MyValidationGroup"/>

Next, in your application code, when an error occurs : set the CustomValidator’s ErrorMessage and IsValid properties :


ServerErrorCustomValidator.ErrorMessage = "Server error :  error description";
ServerErrorCustomValidator.IsValid = false;

C#, check if a string is null or empty

Posted December 7th, 2009 in .NET, Development, Tips and Tricks by Sam Beauvois

In C# you can check if a string is null or empty in many ways :

string String1 = "testString";
if (String1== null || String1 == "")
{
// ...
}
else
{
// ...
}

string String1 = "testString";
 if (String1 == null || String1.Length==0)
 {
 // ...
 }
 else
 {
 // ...
 }
 string String1 = "testString";
 if (string.IsNullOrEmpty(String1))
 {
 // ...
 }
 else
 {
 // ...
 }

The last one is more readable.

But what about performances ?

Check this code :

private void CheckForNullOrEmpty()
 {
 int maxIterations = 9999999;

 Console.WriteLine("if (string.IsNullOrEmpty(String1))");
 Stopwatch watch = Stopwatch.StartNew();
 for (int i = 0; i < maxIterations; i++)
 {
 string String1 = "testString";
 if (string.IsNullOrEmpty(String1))
 {
 // ...
 }
 else
 {
 // ...
 }
 }

 watch.Stop();
 Console.WriteLine("\tTime : " + watch.Elapsed.TotalMilliseconds);
 Console.WriteLine(Environment.NewLine);

 Console.WriteLine("if (String1== null || String1 == \"\"");
 watch = Stopwatch.StartNew();
 for (int i = 0; i < maxIterations; i++)
 {
 string String1 = "testString";
 if (String1 == null || String1 == "")
 {
 // ...
 }
 else
 {
 // ...
 }
 }

 watch.Stop();
 Console.WriteLine("\tTime : " + watch.Elapsed.TotalMilliseconds);
 Console.WriteLine(Environment.NewLine);

 Console.WriteLine("if (String1 == null || String1.Length==0)");
 watch = Stopwatch.StartNew();
 for (int i = 0; i < maxIterations; i++)
 {
 string String1 = "testString";
 if (String1 == null || String1.Length == 0)
 {
 // ...
 }
 else
 {
 // ...
 }
 }

 watch.Stop();
 Console.WriteLine("\tTime : " + watch.Elapsed.TotalMilliseconds);
 Console.WriteLine(Environment.NewLine);

 Console.WriteLine(Environment.NewLine);
 Console.WriteLine("Press any key to continue");
 Console.ReadLine();
 }

Output is :

CheckStringForNullOrEmptyOutput

The difference between “String.IsNullOrEmpty” and “check for null then for lenght” is minimal, so prefer the String.IsNullOrEmpty() wich is more readable.

As you can guess, IsNullOrEmpty performs a check for null, then a check for lenght :

If you use Reflector to inspect the String class in the mscorlib, you can see that String.IsNullOrEmpty do the same thing we do in our test :

public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}

the “extra” time comes from the method call

Note : In .Net Framework 4.0: string.IsNullOrWhiteSpace() method appears, but I've not tested yet (link)

How long need a portion of code to be executed ?

Posted December 4th, 2009 in .NET, Development, Source Code, Tips and Tricks by Sam Beauvois

Use the Stopwatch object !

using System.Diagnostics;

// ...

Stopwatch myStopWatch = Stopwatch.StartNew();

// some code

myStopWatch.Stop();

Console.WriteLine("{0} ms - {1} ticks", myStopWatch.Elapsed.TotalMilliseconds, myStopWatch.ElapsedTicks);

// ...