How to pass data from one page to another page using querystring?

Posted by in ASP.NET category on for Advance level | Points: 250 | Views : 87297 red flag
Rating: 5 out of 5  
 2 vote(s)

To pass value from one page to another page using querystring and accessing that value to retrieve database information, we can follow this approach.


Querystring is suffixed with the url of the page separated by “?” (question mark) in the form of key and value. Below code snippet, we have “autoid” and “com” querystring with “8” and “show” value respectively.


Note: In case we want to pass multiple querystring, they are separated by “&” (Ampersand symbol)



<a href="ShowDetails.aspx?autoid=5" title="Show records where AutoId is 5">Show records where AutoId is 5 </a>

<p><asp:HyperLink ID="hyper1" runat="server" Text="Show Record where AutoID is 8"> </asp:HyperLink> </p>



protected void Page_Load(object sender, EventArgs e)


if (!IsPostBack)


hyper1.NavigateUrl = "ShowDetails.aspx?autoid=8&com=show";




In the default.aspx page we have an anchor tag and an asp:HyperLink control with two querystrings.

This solution is one of hundreds of ASP.NET How to's solutions available here.



<asp:DetailsView ID="DetailsView1" runat="server" EnableViewState="false" />




protected void Page_Load(object sender, EventArgs e)


if (!IsPostBack)


if (!string.IsNullOrWhiteSpace(Request.QueryString["autoid"]))


int autoId = 0;

int.TryParse(Request.QueryString["autoid"], out autoId);

if (!autoId.Equals(0))





// to get the other querystring do the same

string command = Request.QueryString["com"];



private void GetData(int autoId)


DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))


// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail WHERE AutoId = @AutoId ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))


// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))


SqlParameter prm = new SqlParameter("@AutoId", SqlDbType.Int);

prm.Value = autoId;


// fire Fill method to fetch the data and fill into DataTable





// specify the data source for the GridView

DetailsView1.DataSource = table;

// bind the data now



On click of the hyperlink on the default.aspx, user is redirected to ShowDetails.aspx page with querystring values. That querystring value is retrieved into the Page_Load event of ShowDetails.aspx and passed to GetData method. GetData method uses ADO.NET to retrieve the values from the database and populates to the DetailsView.


Notice the status bar where url has “autoid=5” separated by “?”.

Clicking above links show the respective record as shown below


Querystring is the easiest way to pass very small amount of data from one page to another page as it passes through the url of the page. However, as it is exposed to the user via the url, it is not suggested to pass the sensitive information like SSL, DOB, PAN Number etc via querystring. 

Do let me know your feedback or question about this article. Thanks for reading and happy learning.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: Chvrsri on: 6/10/2011 | Points: 25
HI Sheo ,

Great post ! Explanation flow is awesome.

You talked about passing sensitive information through query string but how about using URL Rewritting ?

Is there any way that we can hide the (Sensitive) info using this approach ?
Posted by: SheoNarayan on: 6/10/2011 | Points: 25
No, you can't. Even if you use URL Rewriting, your info exists in the URL. The benefit URL Rewriting gives is that it makes the url looks good, easily readable, understandable and meaningful.

In case you want to pass the sensitive data, I would recommend you to use Session variable not even Cookie as cookie is saved into client's machine.


Posted by: Chvrsri on: 6/10/2011 | Points: 25
Ya that's fine as we also hide our page extension in url rewritting for example we have a page called , but we can make that as

So, i thought in that way. Any how thanq for clarifying me !!
Posted by: SheoNarayan on: 6/10/2011 | Points: 25
Yes, Cool.

Posted by: Susanthampy on: 6/13/2011 | Points: 25
simply gud
Posted by: Akiii on: 6/15/2011 | Points: 25
excellent article sir....
keep posting....

Thanks and Regards
Posted by: Anilbabu.M on: 8/2/2012 | Points: 25
What is URL REDIRECTION?How can i use this concept in my .net?
my task is i have generated one url like this ""
I am passing one pearameter like ""
I want to display Ename in Database table that corresponding "Empno"
plz Help me Give me one simple example

I am new this concepts
plz Help me send source code to my

Login to post response

Comment using Facebook(Author doesn't get notification)