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
2010-05-12 07:24:05
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
2010-05-12 07:26:22
A:
Request.QueryString gives you array of string variables sent to the client
Request.QueryString["QOT_ID"]
See documentation
Myra
2010-05-12 07:27:05
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
2010-05-12 07:30:04
`Request.QueryString[]` is guaranteed not to return `null`.
egrunin
2010-05-12 08:07:20
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
2010-05-12 08:28:18
Yes, you're right.
egrunin
2010-05-13 02:58:11
+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
2010-05-12 07:42:55