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.

Using Google Custom Search Engine (CSE) with Asp .Net

I’ve been using Google Custom Search Engine (CSE) for one of my projects (searchTravelBlog). When I was trying to implement CSE in NewGuid.NET I found some problems doing this.The big issue is that CSE depends on the html FORM element with submit event for posting all the variables from the hiddenfields. 

The CSE code from Google looks like this:   

[code:html]
<form action="http://" id="searchbox_004900934070880588383:m5qbf6oxe6k">
      <input type="hidden" name="cx" value="004900934070880588383:m5qbf6oxe6k" />
      <input type="hidden" name="cof" value="FORID:11" />
      <input type="text" name="q" size="25" />
      <input type="submit" name="sa" value="Search" />
</form>
[/code]

 

When you copy this code into a .Net page it will not work. This because of the default .Net server form tag.

If you want to use CSE within your .Net page don't use Google's code that generates the search form.  Create your own code, just add one Textbox and one Button control to your page.

[code:html]
<asp:TextBox ID="searchQuery" runat="server" size="30" />
<asp:Button runat="server" Text="Search .Net Info" ID="searchButton" OnClick="searchButton_Click" />
[/code]

 

In the code behind you add the following searchButton_Click event:

[code:c#]
protected void searchButton_Click(object sender, EventArgs e)
{
   StringBuilder redirectUrl = new StringBuilder();
   //The URL to your resultpage
   redirectUrl.Append("./search_netInfo.aspx");
   //Add your CSE unique identifier
   redirectUrl.Append("?cx=004900934070880588383%3Am5qbf6oxe6k");
   //Add your advertising location code
   redirectUrl.Append("&cof=FORID%3A11");
   //The search query
   redirectUrl.Append("&q=" + searchQuery.Text);
   //Redirect to the resultpage
   Response.Redirect(redirectUrl.ToString());
}
[/code]

 

You can find these values in the hidden values from the default code that Google creates for you.It is important that the values within the redirectUrl are html encoded.

Add the following code to your Page_PreRender so that the searchquery will appear in the textbox.

[code:c#]
protected void Page_PreRender(object sender, EventArgs e)
{
   searchQuery.Text = Request.QueryString["q"];
}
[/code]

 

Note: This example doesn't include the branding that Google requires. Please read the branding guidelines here!

obout Suite for .Net 2.0

When surfing around I found this great range ASP.Net controls.  Obout.com offers a great variety of ASP and ASP .Net controls. The controls are Cross-Browser (including Safari  and Opera).You can download the obout Suite for .Net 2.0. The suite contains the following controls:

  • TreeView  (recommended)
  • Grid
  • Editor
  • Spell checker
  • Calendar
  • Easy menu
  • Combobox
  • Slide menu
  • AJAXPage
  • Two colors menu
  • Splitter
  • Super button
  • Tree_DB
  • Autosuggest
  • Show
  • Flyout Window
  • FileUploadProgress
  • Slide Panel (with Slide menu)
  • TabStrip (with Easy Menu) 
  • Context menu (with EasyMenu)
  • Toolbar (with Supper button)
  • State Selector (with Combobox)

There are loads of examples on their website. So go check it out!