String Extension Method: IsGuid()

I wrote an extension method for the string type. With this method you can check if an string is an Guid. The method returns a Bool.

[code:c#]

public static class StringExtensions
{
  public static bool IsGuid(this string input)
  {
    Regex isGuid = new Regex(@"^({){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$", RegexOptions.Compiled);
    bool isValid = false;
    if (input != null)
    {
      if (isGuid.IsMatch(input))
      {
        isValid = true;
      }
    }
  return isValid;
  }
}

[/code]

If you're wondering I didn't wrote the Regex myself 🙂 (but it works)

This brute-force method is mutch faster (thanks to Arjan and Tag for the comments):

[code:c#]

public static bool IsGuid(this string input)
{
try
{
new Guid(input);
return true;
}
catch (ArgumentNullException) {}
catch (FormatException) {}
catch (OverflowException) {}
return false;
}

[/code]

 

A good example how extension methods can make some things easier. 

Hope it helps!

Using Session with a Web Handler File (ashx)

In on of our project we use ASHX file to dynamicly generate stylesheets depending on the user. An problem occurred when the userdata was loaded from the Session. The Session object was NULL.

After searching the internet I found the solution for this problem. You need to implement the folowing interfaces on the ASHX;  IRequiresSessionState and IReadOnlySessionState. See example below.

[code:c#]

public class Style : IHttpHandler, IRequiresSessionState, IReadOnlySessionState {}

[/code]

 

Dynamic load whereparameters for linqDatasource

When using an Linq Datasource (linq to sql) for a datagrid (Gridview, ListView, etc) you don't always want all rows from a table. To exclute data you can use the WhereParameters to add an where statement to your DataSource.

You can do limited where statements with the Visual Studio Wizards. When you need more than a limited statement you can dynamicly create an Parameter to add to your Linq DataSource.

The following examples shows how to filter the Linq Datasource that recovers all rows from the Post table and filters them on the BlogId. The BlogManager.CurrentBlogId gets the blog GUID from the Session.

[code:c#]

   Parameter whereparam = new Parameter();
   whereparam.Name = "BlogId";
   whereparam.DefaultValue = BlogManager.CurrentBlogId.ToString();
   whereparam.Type = TypeCode.Object;
   linqDataSource.WhereParameters.Add(whereparam);
   linqDataSource.Where = "BlogId == Guid(@BlogId)";

[/code]


You cast the @BlogId (string) to a Guid In the linqDataSource.Where property otherwise you get the following error:

Operator '==' incompatible with operand types 'Guid' and 'String'

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Query.Dynamic.ParseException: Operator '==' incompatible with operand types 'Guid' and 'String'

101 Linq samples

When learning and working with Linq. I had the need of reference samples. When searching for een example of the select syntax I stumbled on the follow site from Microsoft:

101 Linq Samples

It's a good reference guide for al your basic Linq query's.

Portofolio: MCPD certification course

To get more developers certified I started a MCTS learning course for the developers. Once in the two weeks all we had half a day to review two chapters, every chapter was reviewed by a developer.

Ten developers started the course and at this moment (20 April) six people are MCTS (Web) certified and one MCPD (and two employees left).

How to create a iGoogle Widget!

Because every eveloper needs to have a New Guid generator and .Net info search iGoogle widget ;-). It was time for me to create one!

I had no idea where to start, try searching Google no good results. But after clicking along on iGoogle I found the Google Gadgets developer site. After reading some documents and playing with some examples I found out that it is to easy to create a iGoogle widget. Here is a … step guide to create a iGoogle widget.

In this guide where going to load an external website (http://www.newguid.net/iGoogle_CreateGuid.aspx) in the widget (the most easy way to build a widget).

Step 1:
Go to the Google getting started document and scroll down to the Google Gadgets Editor. Here you can try out some examples or insert your own code.

Step 2:
Insert the following code to the Gadgets Editor 

[code:html]

<?xml version=”1.0″ encoding=”UTF-8″?>
<Module>
  <ModulePrefs title=”[Widget Title]” height=”100″ author=”[Author Name]” author_email=”[Author Email]” description = “[Widget Description]” screenshot = “[Link to a Screenshot]” thumbnail = “[Link to a thumbnail]” author_link = “[Link to author website]”><Require feature=”dynamic-height”/>
  </ModulePrefs>
  <Content type=”url” href=”http://www.newguid.net/iGoogle_CreateGuid.aspx”/>
</Module>

[/code]

Press the Preview tab. You will see that the New Guid widget is already working. The widget just loads the website within an Iframe. The best thing is that you only have to design an build your widget once on your own server. It is a normal page so there aren’t any design limits.

Step 3:
Save and Publish your widget. You have to change all properties (eg. title, author, author_email, etc.) in de code before you save and publish your widget. After you’ve changed the properties you can just save and then publish your widget. You can either publish your widget on your webpage (just by linking to it) or publish it into the Google Directory or both.

Publish your gadget in the editor by going to File > Publish. This button is only clickable if your syntax is correct.

Your gadget will be validated before the real publish. Fix all the issues that show in popup (see image below). And retry.

 

 

Like I told you to easy. Ofcourse you can do mutch more with it, but this will do for most of your widgets.