Hi RichardSa,
Below, I am fetching the records from Table based on Order Id value fetched from Session.
Please refer below Sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Default
<table>
<tr>
<th>Order Id:</th>
<td><asp:TextBox ID="txtId" runat="server" /></td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<asp:Button Text="Submit" ID="btnSubmit" runat="server" OnClick="Submit" /></td>
</tr>
</table>
Home
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Order Id" DataField="OrderId" />
<asp:BoundField HeaderText="Ship Name" DataField="ShipName" />
<asp:BoundField HeaderText="Ship City" DataField="ShipCity" />
<asp:BoundField HeaderText="Ship Country" DataField="ShipCountry" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
Default
protected void Submit(object sender, EventArgs e)
{
Session["OrderId"] = int.Parse(txtId.Text);
Response.Redirect("~/Home.aspx");
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
int orderId = int.Parse(Session["OrderId"].ToString());
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT OrderId, ShipName, ShipCity, ShipCountry FROM Orders WHERE OrderId = @OrderId", con))
{
cmd.Parameters.AddWithValue("@OrderId", orderId);
con.Open();
this.gvOrders.DataSource = cmd.ExecuteReader();
this.gvOrders.DataBind();
con.Close();
}
}
}
VB.Net
Default
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Session("OrderId") = Integer.Parse(txtId.Text)
Response.Redirect("~/Home.aspx")
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim orderId As Integer = Integer.Parse(Session("OrderId").ToString())
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT OrderId, ShipName, ShipCity, ShipCountry FROM Orders WHERE OrderId = @OrderId", con)
cmd.Parameters.AddWithValue("@OrderId", orderId)
con.Open()
Me.gvOrders.DataSource = cmd.ExecuteReader()
Me.gvOrders.DataBind()
con.Close()
End Using
End Using
End Sub
Screenshot