• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

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.

0
kwh3856
Asked:
kwh3856
  • 7
  • 3
1 Solution
 
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
 
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
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
 
abelCommented:
> 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
 
kwh3856Author Commented:
Thanks for you help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now