Hi ahmadsubuhanl,
Please refer below sample.
SQL
CREATE TABLE [UserActivity_Login]
(
[UserId] VARCHAR(20) Not Null
,[Employee_No] VARCHAR(20) Not Null
,[Timestamp] DATETIME Not Null
,[Function_Performed] NVARCHAR(100) Not Null
)
CREATE TABLE [Login]
(
[User_Name] VARCHAR(20) NULL
,[Login_Name] VARCHAR(20) NULL
,[Employee_No] VARCHAR(10) NULL
,[Login_Password] VARCHAR(20) NULL
,[Type] NVARCHAR(20) NULL
,[Status] VARCHAR(20) NULL
)
HTML
Login
<h2 class="form-signin-heading">LOGIN</h2>
<div id="dvMessage" runat="server" visible="false" class="alert alert-danger">
<strong><i class="fad fa-exclamation-square" aria-hidden="true"></i> </strong>
<asp:Label ID="lblMessage" runat="server" />
</div>
<label for="txtUserName">UserName</label>
<asp:TextBox ID="txtUserName" runat="server" /><br />
<br />
<label for="txtPassword">Password</label>
<asp:TextBox ID="txtPassword" runat="server" /><br />
<a href="#">Forgotten Password?</a>
<br />
<br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="OnLogin" />
<br />
Home
<table>
<tr>
<td><asp:Button ID="btnAddNew" runat="server" Text="Add New" OnClick="OnAddNew" /></td>
</tr>
<tr>
<td><asp:Button ID="btnSave" runat="server" Text="Save" OnClick="OnSave"/></td>
</tr>
<tr>
<td><asp:Button ID="btnDiscard" runat="server" Text="Discard" OnClick="OnDiscard" /></td>
</tr>
<tr>
<td><asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="OnLogout" /></td>
</tr>
</table>
Namespaces
Login
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Home
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
C#
Login
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 Login_Name, Login_Password,Employee_No From Login WHERE Login_Name = @Login_Name and Login_Password = @Login_Password", con))
{
cmd.Parameters.AddWithValue("@Login_Name", txtUserName.Text);
cmd.Parameters.AddWithValue("@Login_Password", txtPassword.Text);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Session["Login_Name"] = sdr["Login_Name"];
Session["Employee_No"] = sdr["Employee_No"];
}
Response.Redirect("Home.aspx");
con.Close();
}
}
}
Home
protected void OnAddNew(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivity_Login VALUES ('" + Session["Login_Name"].ToString() + "','" + Session["Employee_No"].ToString() + "',getdate(),'Add New Button Click')", con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
protected void OnSave(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivity_Login VALUES ('" + Session["Login_Name"].ToString() + "','" + Session["Employee_No"].ToString() + "',getdate(),'Save Button Click')", con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
protected void OnDiscard(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivity_Login VALUES ('" + Session["Login_Name"].ToString() + "','" + Session["Employee_No"].ToString() + "',getdate(),'Discard Button Click')", con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
protected void OnLogout(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivity_Login VALUES ('" + Session["Login_Name"].ToString() + "','" + Session["Employee_No"].ToString() + "',getdate(),'Logout Button Click')", con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Screenshot
Output
UserId |
Employee_No |
Timestamp |
Function_Performed |
Mudassar |
5 |
55:30.0 |
Add New Button Click |
Mudassar |
5 |
55:30.8 |
Save Button Click |
Mudassar |
5 |
55:31.3 |
Discard Button Click |
Mudassar |
5 |
55:31.9 |
Logout Button Click |
User_Name |
Login_Name |
Employee_No |
Login_Password |
Type |
Status |
Mudassar_Khan |
Mudassar |
5 |
12345 |
A |
Active |