2

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 5

5

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

I believe you meant Uri uri = new Uri(url);?
0

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

0

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

Request.QueryString["QOT_ID"] 

See documentation

Comments

0

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.

2 Comments

Request.QueryString[] is guaranteed not to return null.
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 ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.