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.