Solved

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

Posted on 2009-05-15
4
862 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 50
asp.net mvc input file 2 32
How do ASP.NET and MVC work together? 4 30
Ajax Panel Not Updating When Triggered By Timer 4 46
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

23 Experts available now in Live!

Get 1:1 Help Now