Hi muhammad12,
Please refer below sample.
SQL
CREATE TABLE [tblStation]
(
[Flight_id] INT ,
[Name] VARCHAR (10),
[Origin] VARCHAR (10),
[Destination] VARCHAR (10),
[Date] VARCHAR (10),
)
INSERT INTO [tblStation] VALUES (1357,'Na201','SHJ','DXB','05-09-2022')
INSERT INTO [tblStation] VALUES (18449,'Sa207','DXB','RXD','05-09-2022')
CREATE TABLE [Session]
(
[SessionId] VARCHAR (50) ,
[UserId] VARCHAR (10),
[FlightId] VARCHAR (10),
[Status] VARCHAR (10),
)
HTML
Default
<table>
<tr>
<td>User Name:</td>
<td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="OnLogin" /></td>
</tr>
</table>
Home
<asp:GridView ID="gvFlights" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField HeaderText="Flight_id" DataField="Flight_id" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Origin" DataField="Origin" />
<asp:BoundField HeaderText="Destination" DataField="Destination" />
<asp:BoundField HeaderText="Date" DataField="Date" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" OnClick="OnSelect" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblUserName" runat="server" />
Login
<b>UserName:</b>
<asp:Label ID="lblUserId" runat="server" /><br />
<b>Session Id:</b>
<asp:Label ID="lblSession" runat="server" />
Namespace
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 OnLogin(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Origin FROM [UserLogin] WHERE UserName = @UserName AND Password = @Password", con))
{
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
string origin = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (!string.IsNullOrEmpty(origin))
{
Session["User"] = txtUserName.Text;
Session["Origin"] = origin;
Response.Redirect("CS.aspx");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Invalid UserName or Password.')", true);
}
}
}
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string origin = Session["origin"].ToString();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [tblStation] WHERE Origin = @Origin ", con))
{
cmd.Parameters.AddWithValue("@Origin", origin);
con.Open();
gvFlights.DataSource = cmd.ExecuteReader();
gvFlights.DataBind();
con.Close();
}
}
}
private void GetData()
{
//lblUserName.Text = Session["User"].ToString();
gvFlights.DataSource = Session["Flights"] as DataTable;
gvFlights.DataBind();
}
protected void OnSelect(object sender, EventArgs e)
{
string userId = Session["User"].ToString();
string sessionId = Session.SessionID;
string status = "Active";
//DataTable dt = Session["Flights"] as DataTable;
string flightId = ((sender as Button).NamingContainer as GridViewRow).Cells[2].Text;
//dt.Select("Flight_Id='" + flightId + "'")[0]["Status"] = "Active";
//Session["Flights"] = dt;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Session VALUES (@SessionId, @UserId, @FlightId, @Status)", con))
{
cmd.Parameters.AddWithValue("@SessionId", sessionId);
cmd.Parameters.AddWithValue("@UserId", userId);
cmd.Parameters.AddWithValue("@FlightId", flightId);
cmd.Parameters.AddWithValue("@Status", status);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect("Home.aspx");
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[4].Text == "Active")
{
e.Row.BackColor = Color.Green;
(e.Row.FindControl("btnSelect") as Button).Enabled = false;
}
}
}
Login
protected void Page_Load(object sender, EventArgs e)
{
lblUserId.Text = Session["User"].ToString();
lblSession.Text = Session.SessionID;
}
VB.Net
Default
Protected Sub OnLogin(ByVal sender As Object, ByVal e As EventArgs)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT Origin FROM [UserLogin] WHERE UserName = @UserName AND Password = @Password", con)
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim())
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim())
con.Open()
Dim origin As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
If Not String.IsNullOrEmpty(origin) Then
Session("User") = txtUserName.Text
Session("Origin") = origin
Response.Redirect("CS.aspx")
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Invalid UserName or Password.')", True)
End If
End Using
End Using
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 origin As String = Session("origin").ToString()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM [tblStation] WHERE Origin = @Origin ", con)
cmd.Parameters.AddWithValue("@Origin", origin)
con.Open()
gvFlights.DataSource = cmd.ExecuteReader()
gvFlights.DataBind()
con.Close()
End Using
End Using
End Sub
Private Sub GetData()
gvFlights.DataSource = TryCast(Session("Flights"), DataTable)
gvFlights.DataBind()
End Sub
Protected Sub OnSelect(ByVal sender As Object, ByVal e As EventArgs)
Dim userId As String = Session("User").ToString()
Dim sessionId As String = Session.SessionID
Dim status As String = "Active"
Dim flightId As String = (TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)).Cells(2).Text
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Session VALUES (@SessionId, @UserId, @FlightId, @Status)", con)
cmd.Parameters.AddWithValue("@SessionId", sessionId)
cmd.Parameters.AddWithValue("@UserId", userId)
cmd.Parameters.AddWithValue("@FlightId", flightId)
cmd.Parameters.AddWithValue("@Status", status)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Response.Redirect("Home.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(4).Text = "Active" Then
e.Row.BackColor = Color.Green
TryCast(e.Row.FindControl("btnSelect"), Button).Enabled = False
End If
End If
End Sub
Login
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
lblUserId.Text = Session("User").ToString()
lblSession.Text = Session.SessionID
End Sub
Screenshot
OutPut