Hi muhammad12,
Please refer below sample.
SQL
CREATE TABLE [Flights]
(
[User_Id] INT,
[User_Name] VARCHAR(20),
[Flight_Id] INT,
[Flight_No] INT,
[Flight_Date] VARCHAR(20),
[Origin] VARCHAR(20),
[Status] VARCHAR(20)
)
INSERT INTO [Flights] VALUES(1, 'User-A', 11, 10101, '10/10/2022', 'United States', 'Active')
INSERT INTO [Flights] VALUES(2, 'User-B', 22, 10102, '11/10/2022', 'India', 'In-Active')
INSERT INTO [Flights] VALUES(3, 'User-C', 33, 10103, '12/10/2022', 'Russia', 'In-Active')
INSERT INTO [Flights] VALUES(4, 'User-D', 44, 10104, '13/10/2022', 'France', 'Active')
SELECT [User_Id], [User_Name], [Flight_Id], [Flight_No], [Flight_Date], [Origin], [Status] FROM [Flights]
CREATE TABLE [Session]
(
[User_Id] INT,
[Flight_Id] INT,
[Status] VARCHAR(20)
)
HTML
Default
<asp:GridView ID="gvFlights" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" OnClick="OnSelect" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="User Id" DataField="User_Id" />
<asp:BoundField HeaderText="User Name" DataField="User_Name" />
<asp:BoundField HeaderText="Flight Id" DataField="Flight_Id" />
<asp:BoundField HeaderText="Flight No." DataField="Flight_No" />
<asp:BoundField HeaderText="Flight Date" DataField="Flight_Date" />
<asp:BoundField HeaderText="Origin" DataField="Origin" />
<asp:BoundField HeaderText="Status" DataField="Status" />
</Columns>
</asp:GridView>
Home
<asp:Button Text="Back" runat="server" PostBackUrl="~/Default.aspx" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing
Code
Default
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
GetData();
gvFlights.DataSource = Session["Flights"] as DataTable;
gvFlights.DataBind();
}
private void GetData()
{
DataTable dt = new DataTable();
if (Session["Flights"] != null)
{
dt = Session["Flights"] as DataTable;
}
else
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT [User_Id], [User_Name], [Flight_Id], [Flight_No], [Flight_Date], [Origin], [Status] FROM [Flights]", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
Session["Flights"] = dt;
}
}
}
}
}
protected void OnSelect(object sender, EventArgs e)
{
DataTable dt = Session["Flights"] as DataTable;
string flightId = ((sender as Button).NamingContainer as GridViewRow).Cells[3].Text;
dt.Select("Flight_Id='" + flightId + "'")[0]["Status"] = "Active";
Session["Flights"] = dt;
Response.Redirect("Home.aspx");
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[7].Text == "Active")
{
e.Row.BackColor = Color.Green;
(e.Row.FindControl("btnSelect") as Button).Enabled = false;
}
}
}
VB.Net
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()
GetData()
gvFlights.DataSource = TryCast(Session("Flights"), DataTable)
gvFlights.DataBind()
End Sub
Private Sub GetData()
Dim dt As DataTable = New DataTable()
If Session("Flights") IsNot Nothing Then
dt = TryCast(Session("Flights"), DataTable)
Else
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT [User_Id], [User_Name], [Flight_Id], [Flight_No], [Flight_Date], [Origin], [Status] FROM [Flights]", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
sda.Fill(dt)
Session("Flights") = dt
End Using
End Using
End Using
End If
End Sub
Protected Sub OnSelect(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TryCast(Session("Flights"), DataTable)
Dim flightId As String = (TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)).Cells(3).Text
dt.[Select]("Flight_Id='" & flightId & "'")(0)("Status") = "Active"
Session("Flights") = dt
Response.Redirect("HomeVB.aspx")
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(7).Text = "Active" Then
e.Row.BackColor = Color.Green
TryCast(e.Row.FindControl("btnSelect"), Button).Enabled = False
End If
End If
End Sub
Screenshot