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.
5 Answers
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
}
Comments
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.
1 Comment
Uri uri = new Uri(url);?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.
Comments
Request.QueryString gives you array of string variables sent to the client
Request.QueryString["QOT_ID"]
See documentation
Comments
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.