Hi muhammad12,
Please refer below code and take reference from it and implement in your code.
Database
CREATE TABLE [dbo].[Sessiondata](
[SessionId] [varchar](300) NULL,
[UserId] [varchar](20) NULL,
[FlightId] [varchar](20) NULL,
[Status] [varchar](20) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tblStation](
[flightId] [varchar](20) NULL,
[Name] [varchar](20) NULL,
[Origin] [varchar](20) NULL,
[Destination] [varchar](20) NULL,
[Date] [varchar](20) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'grur1vuhbelbqlygmatf2gkw', N'Admin', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'tsrsxo1cgyz5ewhs1dt4avbx', N'Admin', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'h3bzlxrjyrttwacvwkpdi5lm', N'Muhammad', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'ltmn0aw521cnxl2ssdiivntw', N'Admin', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'ltmn0aw521cnxl2ssdiivntw', N'Muhammad', N'18449', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'hesrlq3wv0puef1il4lmn55r', N'Admin', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'kudh2ekqbeicd1muwhon05ct', N'Admin', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'rsmx4q035he15avyayr5qf14', N'Admin', N'1357', N'Active')
INSERT [dbo].[Sessiondata] ([SessionId], [UserId], [FlightId], [Status]) VALUES (N'fntpxq1xe3xk2401oxfrtkzm', N'Muhammad', N'18449', N'Active')
INSERT [dbo].[tblStation] ([flightId], [Name], [Origin], [Destination], [Date]) VALUES (N'1357', N'Na201', N'SHJ', N'DXB', N'05-09-2022')
INSERT [dbo].[tblStation] ([flightId], [Name], [Origin], [Destination], [Date]) VALUES (N'18449', N'Sa207', N'DXB', N'RXD', N'05-09-2022')
HTML
Defualt
<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="Login" />
</td>
</tr>
</table>
Home
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="StationId" DataField="flightId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Origin" DataField="Origin" />
<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"></asp:Label>
Login
<asp:Label ID="lblUserId" runat="server" />
<asp:Label ID="lblSession" runat="server" />
Namespace
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
Defualt
protected void Login(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Origin FROM Users 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("Home.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)
{
lblUserName.Text = Session["User"].ToString();
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();
gvCustomers.DataSource = cmd.ExecuteReader();
gvCustomers.DataBind();
con.Close();
}
}
}
}
protected void OnSelect(object sender, EventArgs e)
{
string sessionId = Session.SessionID;
string userId = Session["User"].ToString();
string status = "Active";
string flightId = ((sender as Button).NamingContainer as GridViewRow).Cells[0].Text;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Sessiondata 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("Login.aspx");
}
Login
protected void Page_Load(object sender, EventArgs e)
{
lblUserId.Text = Session["User"].ToString();
lblSession.Text = Session.SessionID;
}
VB.Net
Default
Protected Sub Login(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 Users 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("Home.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
lblUserName.Text = Session("User").ToString()
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()
gvCustomers.DataSource = cmd.ExecuteReader()
gvCustomers.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Protected Sub OnSelect(ByVal sender As Object, ByVal e As EventArgs)
Dim sessionId As String = Session.SessionID
Dim userId As String = Session("User").ToString()
Dim status As String = "Active"
Dim flightId As String = (TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)).Cells(0).Text
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Sessiondata 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("Login.aspx")
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