tags:

views:

227

answers:

6

hi,

i have a guid value that i store in my hidden variable. say for eg (303427ca-2a5c-df11-a391-005056b73dd7)

now how do i convert the value from this hidden field back to GUID value (because the method i would be calling expects a GUID value).

thank you.

+2  A: 
    string strGuid;
    strGuid = (your guid here);
    Guid guid = new Guid(strGuid);

For more info, MSDN

curtisk
+2  A: 

Guid has a constructor for string Guids.

Guid guid = new Guid(myStringGuid);
Brian R. Bondy
+2  A: 

new Guid(myHiddenFieldString)

Hasan Khan
+11  A: 

Just use the overloaded constructor:

try
{
  Guid guid = new Guid("{D843D80B-F77D-4655-8A3E-684CC35B26CB}");
}
catch (Exception ex) // There might be a more appropriate exception to catch
{
  // Do something here in case the parsing fails.
}
ereOn
this definitely would be the simplest method
espais
@espais: Yes, I am sometimes scared when I see how easy thoses things have become.
ereOn
Worth noting that there is no TryParse available for GUID so you may want to wrap this in a try/catch block.
Peter Kelly
Thanks @Peter Kelly for pointing that out. I updated my answer to add this advice.
ereOn
+1  A: 

I think it can be done simply as following:

Guid MyGuid = new Guid(stringValue);
sashaeve
+3  A: 

You are making it pretty easy on an attacker by storing the Guid in a string. Trivial to find back in, say, the paging file. Store it in a Guid and kill two birds with one stone.

Hans Passant
+1 for a very good point
espais