Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-15
4
Medium Priority
?
871 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:svelluto
ID: 24397016
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
ID: 24397200
let me try.
0
 
LVL 7

Accepted Solution

by:
ajolly earned 2000 total points
ID: 24398143
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
ID: 24398602
I did id.Length == 36 but i think ajolly's method might be better. Let me try.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

610 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