Solved

Anyway to check if an 'id" is a GUID?

Posted on 2009-05-15
4
861 Views
Last Modified: 2012-05-07
Anyway to check and see if this "id" actually contains a GUID. Sometimes the ID holds ";nbsp" and code fails.

 if (e.Item is GridEditableItem)
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;

                string id = editedItem["Id"].Text;
             
                Basket basket = Basket.Items.FindById(new Guid(id)); //**fails here if ID is not GUID
0
Comment
Question by:Camillia
  • 2
4 Comments
 
LVL 7

Expert Comment

by:svelluto
Comment Utility
Why don't you check the length of the 'id' string after you set it.

View Code Sample.

You could even check if the id string is the same length as the GUID.

Or if you would like to use REGEX to test for a GUID,
here is a good example:

http://geekswithblogs.net/jawad/archive/2005/05/20/GuidVerifier.aspx



if (id.length > 6) //  - 6 Characters

{
 

Basket basket = Basket.Items.FindById(new Guid(id));
 

}
 

//Another Example
 

if (id.length == guid.length) //guid.length could be a number

{
 

Basket basket = Basket.Items.FindById(new Guid(id));
 

}

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
let me try.
0
 
LVL 7

Accepted Solution

by:
ajolly earned 500 total points
Comment Utility
Try this
public static bool isValidGuid(String guid)

{

    bool isValid = false;

    Regex re = 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.IgnoreCase);

    if (re.IsMatch(guid))

    {

        isValid = true;

    }

    return isValid;

}

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
I did id.Length == 36 but i think ajolly's method might be better. Let me try.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now