krolik1991
krolik1991

Reputation: 244

Get parameter from URL on aspx page

How can I get parameter from URL on aspx page?

I have an application which has parameter ?id=12345abcd and I want to get this id to select command in my gridview

 SelectCommand="SELECT [a], [b], [c], [d], [e], [f], [g], [h], [c] FROM [table] WHERE [c] = parameterFromUrl" 

And insert this id from url inside variable paramterFromUrl in query.

Upvotes: 2

Views: 12521

Answers (2)

Stefano Lonati
Stefano Lonati

Reputation: 704

Try this:

sqlCommand.CommandText = "SELECT [a], [b], [c], [d], [e], [f], [g], [h], [c] FROM [table] WHERE [c] = @ParameterFromUrl";
sqlCommand.Parameters.AddWithValue("@ParameterFromUrl", Request.QueryString["id"].ToString());

Upvotes: 1

Zippy
Zippy

Reputation: 1824

You can access that Id by looking at the Request.QueryString property in your code. For your use case, in the code behind, access that Id like:

string Id= Request.QueryString["id"].ToString();

And use it in your SelectCommand as:

SelectCommand="SELECT [a], [b], [c], [d], [e], [f], [g], [h], [c] FROM [table] WHERE [c] =" + Id;

Please be aware of sql injection

Upvotes: 5

Related Questions