user3711519
user3711519

Reputation: 11

Get Guid with query string

I get a Guid value in a variable like that

var getvalueGuid = db.Clients.Where(u => u.Numero_telephone == 
TextBox_numero_telephone.Text).Select(u => u.GuID).FirstOrDefault();

And i would like to convert it in a query string like that:

getvalueGuid = Request.QueryString["id"];

How to do?

Upvotes: 1

Views: 4051

Answers (4)

Jesse Petronio
Jesse Petronio

Reputation: 723

Guid requestGuid;
if (Guid.TryParse(Request.QueryString["id"], out requestGuid))
{
   // Do logic here with requestGuid
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can use Guid.TryParse:

Guid getvalueGuid;
if(Guid.TryParse(Request.QueryString["id"], out getvalueGuid))
{
    // successfully parsed 
}

Upvotes: 1

Dai
Dai

Reputation: 155145

It's hard to understand your question as you're missing a lot of detail, but I think you want to get a strongly-typed GUID value from the querystring?

System.Guid doesn't have a TryParse method, so you'll have to use the constructor and catch any exceptions thrown:

If so, then do this:

String guidStr = Request.QueryString["id"];
Guid guid = null;
try {
    guid = new Guid( guidStr );
} catch(ArgumentNullException) {
} catch(FormatException) {
} catch(OverflowException) {
}

if( guid == null { 
    // Inform user that the GUID specified was not valid.
}

The three exceptions (ArgumentNullException, FormatException, and OverflowException are documented in the notes for the Guid(String) constructor here: http://msdn.microsoft.com/en-us/library/96ff78dc%28v=vs.110%29.aspx

Update:

I forgot that .NET 4.0 introduced the TryParse method. Use that instead if you're using .NET 4.0 or later: http://msdn.microsoft.com/en-us/library/system.guid.tryparse%28v=vs.110%29.aspx

Upvotes: 0

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

You will be able to get that inside a QueryString, only if you're having the url like

www.example.com/page?id=[guid_here]

Then when you'll use the code, it would provide you with a String which would contain the Query String provided in the URL.

Upvotes: 0

Related Questions