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

Sheonarayan
Posted by in ASP.NET category on for Advance level | Points: 250 | Views : 86692 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.

Introduction

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.

ShowDetails.aspx?autoid=8&com=show 

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

DEFAULT.ASPX

<div>

<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>

</div>

DEFAULT.ASPX.CS CODE BEHIND

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.

SHOWDETAILS.ASPX

<div>

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

</div>

SHOWDETAILS.ASPX.CS CODE BEHIND

 

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))

{

GetData(autoId);

}

}

// 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;

cmd.Parameters.Add(prm);

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

ad.Fill(table);

}

}

}

// specify the data source for the GridView

DetailsView1.DataSource = table;

// bind the data now

DetailsView1.DataBind();

}

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.

OUTPUT

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

Clicking above links show the respective record as shown below


Conclusion

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

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

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.

Thanks


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 dotnetfunda.com/forums.aspx , but we can make that as dotnetfunda.com/forums/....

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

Thanks
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
Akiii
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 "http://example.com/"
I am passing one pearameter like "http://example.com/Empno=1"
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 mailID:mandla.anilbabu@gmail.com

Login to post response

Comment using Facebook(Author doesn't get notification)