tags:

views:

92

answers:

5

I have a URL http://localhost/TradeCredits/UnderWriter/EditQuote.aspx?QOT_ID=1 I want to fetch QOT_ID from URL . Please suggest how to do that.

+3  A: 

You can use this line of code:

int id = Convert.ToInt32(Request.QueryString["QOT_ID"]);

Or this if you want to do proper checking:

int id;
if (int.TryParse(Request.QueryString["QOT_ID"], out id)) {
    // Do something with the id variable
} else {
    // Do something when QOT_ID cannot be parsed into an int
}
Amry
A: 

The Request.QueryString collection contains all the URL parameters - each can be accessed by name:

var val = Request.QueryString["QOT_ID"];

All returned values are string variables, so you may need to cast to the appropriate type.

Oded
A: 

Request.QueryString gives you array of string variables sent to the client

Request.QueryString["QOT_ID"] 

See documentation

Myra
A: 

QOT_ID could be null or a combination of letters

int id = Request.QueryString["QOT_ID"] != null && Int32.TryParse(Request.QueryString["QOT_ID"]) ? int.Parse(Request.QueryString["QOT_ID"]) : -1;

Not 100% sure if you can safely pass a null value to Int32.TryParse.

Fabian
`Request.QueryString[]` is guaranteed not to return `null`.
egrunin
I was surprised by this since i always check for null in my current projects, so i tested this. Request.QueryString does infact return null if the index/name you pass doesn't exist ...
Fabian
Yes, you're right.
egrunin
+2  A: 

If you have an URL as you mentioned in your question which may not be connected to the current request you could do it like this:

string url = "http://localhost/TradeCredits/UnderWriter/EditQuote.aspx?QOT_ID=1";
Uri uri = new Uri(url);
var parameters = HttpUtility.ParseQueryString(uri.Query);
var id = parameters["QOT_ID"];

and id variable holds your parameter's value.

Alex
I believe you meant `Uri uri = new Uri(url);`?
Amry
@Amry, sure, fixed, thanks!
Alex