How do you convert a string variable to a GUID

I am passing a variable between pages using

MPI = Session.Contents["MPI"].ToString();

The actual data in the MPI variable is a guid.  I need to reuse the variable as a GUID.  How do I now convert the variable back to a GUID type.  I tried using Covert.xxx but there was no Convert.ToGuid.

kwh3856Asked:
Who is Participating?
 
abelConnect With a Mentor Commented:
> Is it possible to do this?

That was your original question, and I showed you ways of doing this, what is the problem with the methods I showed? Please explain any errors you receive In practice, it really is only one line (see my first comment), why does it not work for you? Can you paste your code?

MPI = Session.Contents["MPI"].ToString();Guid guid = new Guid(MPI);
-- Abel --


0
 
abelCommented:
Use the following:

Guid guid = new Guid(MPI);

which works if the string is any of the many popular GUID formats.
0
 
abelCommented:
As an example, in the following code snippet, every guid is the same, based on the three most-used GUID string formats:

Guid guid1 = new Guid("{abc012de-efef-4545-0101-fedcba012345}");
Guid guid2 = new Guid("abc012de-efef-4545-0101-fedcba012345");
Guid guid3 = new Guid("abc012deefef45450101fedcba012345");

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
abelCommented:
If you are unsure about the code input and you want to use a default value, you can wrap it into a function, like this:

private Guid ConvertToGuid(string guidString)
{
    Guid guid1;
    try
    {
        guid1 = new Guid(guidString);
    }
    catch (FormatException)
    {
        // incorrect guid, we're using some default value 
        // (come up with your own here or use Guid.Empty which is all zeroes:
        guid1 = new Guid("{99999999-9999-9999-9999-999999999999}");
    }
}

Open in new window

0
 
abelCommented:
oops, forgot the return, try again:

private Guid ConvertToGuid(string guidString)
{
    try
    {
        return new Guid(guidString);
    }
    catch (FormatException)
    {
        // incorrect guid, we're using some default value 
        // (come up with your own here or use Guid.Empty which is all zeroes:
        return new Guid("{99999999-9999-9999-9999-999999999999}");
    }
}

Open in new window

0
 
kwh3856Author Commented:
Does this change the value of the original GUID?
 
thanks
Kenny
 
0
 
abelCommented:
I don't understand the question. You ask for convert a string, which contains, I assume, the "original guid". Conversion to another type never changes the original type.
0
 
abelCommented:
Of course, the guid string that you provide through your session variable must obey to any of the string formats I showed in the example.
0
 
kwh3856Author Commented:
I have two web pages.  On the first page, I extract the data out of a field in a grid.  It is extracted as a text value.  Although it is text, it is actually a guid.  I then pass that variable to the next page with a session variable.  I now need to convert the variable back to a guid type.  Is it possible to do this?
Thanks
Kenny
 
 
0
 
kwh3856Author Commented:
Thanks for you help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.