Object Oriented Programming Using C#: Part 9

Before reading this article, please go through the following articles:
  1. Object-Oriented Programming Using C#: Part 1
  2. Object-Oriented Programming Using C#: Part 2
  3. Object-Oriented Programming Using C#: Part 3
  4. Object-Oriented Programming Using C#: Part 4
  5. Object-Oriented Programming Using C#: Part 5
  6. Object-Oriented Programming Using C#: Part 6
  7. Object-Oriented Programming Using C#: Part 7
  8. Object-Oriented Programming Using C#: Part 8

User Activity Log Using OOP

 
Key Concept behind the application
 
In this part, we will use a few OOP concepts that we learned earlier. We will create a small application. Please read my article  user-activity-log-using-C-Sharp-with-sql-server/  before continuing. In this session, we will again redesign the same application using the following concepts. It will help us to understand these concepts more clearly plus how to implement them in our own applications.
  1. Class
  2. Member variables
  3. Read-only Properties
  4. Methods
  5. Object
  6. Default Constructor
  7. Constructor Overloading
Application Detail
 
Keep track of user activities in a Database Management System. Especially while working on the Client-server environment where we need to keep track of the System IP/System Name, Login Id, Timestamp, and the action performed on any of the database applications. Either the user edits any client record or does a transition etcetera. 
 
Step 1
 
Create the table with the name User_Activity_Log.
  1. CREATE TABLE [dbo].[User_Activity_Log](  
  2.       [UAL_User_Id] [varchar](20) NOT NULL,  
  3.       [UAL_Timestamp] [datetime] NOT NULL,  
  4.       [UAL_Function_Performed] [nvarchar](100) NOT NULL,  
  5.       [UAL_Other_Information] [nvarchar](100) NULL,  
  6.       [UAL_IP_Address] [nvarchar](15) NOT NULL,  
  7. PRIMARY KEY CLUSTERED  
  8. (  
  9.       [UAL_User_Id] ASC,  
  10.       [UAL_Timestamp] ASC  
  11. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13.    
  14. GO  
  15. SET ANSI_PADDING OFF 
Step 2
 
Create a Login table.
  1. CREATE TABLE [dbo].Login_Client(  
  2.       [LON_User_Name] [varchar](20) NULL,  
  3.       [LON_Login_Name] [varchar](20) NULL,  
  4.       [LON_Employee_No] [varchar](10) NULL,  
  5.       [LON_Login_Password] [varchar](20) NULL,  
  6.       [LON_Type] [nvarchar](20) NULL,  
  7.       [LON_Status] [varchar](20) NULL  
  8. ON [PRIMARY]  
  9.    
  10. GO  
  11. SET ANSI_PADDING OFF 
Step 3
 
Now insert one record in the login table.
  1. INSERT INTO PSH.[dbo].[Login_Client]VALUES('Naveed Zaman','naveed.zaman','22339','khan@123','A','Active'
Step 4
 
Start Visual Studio and create a new Desktop project with the name "UserActivityLog_OOP_Concept".
 
Learn-OOP-using-Csharp1.jpg
 
Step 5
 
First of all, we create the login form that will help us to understand the basic concepts of the topic. Create the form as shown in the picture.
 
Learn-OOP-using-Csharp2.jpg
 
Step 6
 
Create another form with the name of frmActivity.
 
Learn-OOP-using-Csharp3.jpg
 
Step 7
 
Now add a few buttons on it. For example "Add-New", "Save", "Discard" and "Logout" buttons on a form as shown in the following picture.
 
Learn-OOP-using-Csharp4.jpg
 
Step 8
 
Add a new class with the name "Main".
 
Learn-OOP-using-Csharp5.jpg
 
Step 9
 
Now we will create the function GetDBConnection. It is a public function so it can be accessed from anywhere in the project. But you must modify the data source settings relevant to your own settings.
 
For example:
  1. SQL Server instance name. (.\\CRMIS)
  2. SQL Server user name and password. (user id sa password.###Reno123)
  3. Initial Catalog=PSH (database name)
Add the public static class to "Main.cs":
  1. public static SqlConnection GetDBConnection()  
  2. {  
  3.   SqlConnection conn = new SqlConnection(  
  4.   "Data Source=.\\CRMIS;Initial Catalog=PSH;User ID=sa;Password=###Reno321");  
  5.    return conn;  
Step 10
 
Now we will create another class login that will help us to verify the authorization of the user. After providing a username and password.
 
Learn-OOP-using-Csharp6.jpg
 
Step 11
 
Insert the following code in the class login:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SqlClient;  
  6. using System.Data;  
  7.   
  8. namespace UserActivityLog_OOP_Concept {  
  9.     class Login {  
  10.         static string userid;  
  11.         static string password;  
  12.         static string ip;  
  13.         public string EmpNo = string.Empty;  
  14.         SqlConnection con = Main.GetDBConnection();  
  15.   
  16.         public Login() {}  
  17.   
  18.         public Login(string _userid, string _password, string _ip) {  
  19.             userid = _userid;  
  20.             password = _password;  
  21.             ip = _ip;  
  22.         }  
  23.   
  24.         public string getidinfo {  
  25.             get {  
  26.                 return userid;  
  27.             }  
  28.         }  
  29.   
  30.         public string getpassinfo {  
  31.             get {  
  32.                 return password;  
  33.             }  
  34.         }  
  35.         public string getipinfo {  
  36.             get {  
  37.                 return ip;  
  38.             }  
  39.         }  
  40.         public string Validation() {  
  41.             try {  
  42.                 DataTable consultanttable = new DataTable();  
  43.                 string SQL = @ "SELECT LON_Employee_No FROM Login_Client where LON_Login_Name = '" + userid + "' AND LON_Login_Password = '" + password + "'";  
  44.                 SqlDataAdapter Consultantdataadapter = new SqlDataAdapter(SQL, con);  
  45.                 Consultantdataadapter.Fill(consultanttable);  
  46.                 foreach(DataRow myrow in consultanttable.Rows) {  
  47.                     EmpNo = (myrow[0].ToString());  
  48.                 }  
  49.             } catch (InvalidCastException e) {  
  50.                 throw (e); // Rethrowing exception e  
  51.             }  
  52.             return EmpNo;  
  53.         }  
  54.   
  55.     }  
Please check the code carefully.
 
Learn-OOP-using-Csharp7.jpg
 
We have created the class login and in that class, we define four member variables, three of them static and one is public. We have already discussed access modifiers in previous articles. After that, we have created an object icon of the class Main that we defined in Step 6. It will help us to create a connection between C# and SQL Server.
 
After that, we have defined the default constructor of a class Login and overloaded the Constructor with two parameters.
 
It's very important to understand the importance of the overloaded constructor. It will be initialized whenever we create the object of the class with the two parameters, userid, and password. We have defined a default constructor as well that will help us when we need an object on a class without parameters.
 
Learn-OOP-using-Csharp8.jpg
 
Further, we have defined read-only properties of the class login that will help us to read member variables.
 
Learn-OOP-using-Csharp9.jpg
 
In this session, we have defined a method validation of a class login that will help us to validate the login info and return EmpNo.
 
Step 12
 
Please insert the following code in the UserActivityLog class.
 
Learn-OOP-using-Csharp10.jpg
 
Block A
  • We have defined two member variables.
  • We have defined the constructor of the class User_Activity_Log with a two-parameter that will initialize the member variables of the class.
Block B
  • A method task will be used to insert the data into the database, so we need to create the object on the main class with the name of the icon.
  • Using an insert statement we can insert the new row into the database that will get the parameter action which can be "login", "add new", "save" etcetera.
Step 13
 
Please insert the following code in form1.
 
Learn-OOP-using-Csharp11.jpg
Learn-OOP-using-Csharp12.jpg
 
Block A
  • We have initialized the position of the form.
  • String variable host Find host by name System.
  • Gets a list of IP addresses that are associated with a host.
  • Assigns the IP address to CIP.
  • lblCP. Text is assigned the IP address to the label that will display the IP info.
Block B
  • Close the login windows.
Block C
  • Checking if textboxes are not empty.
Block D
  • Creating object LG that will activate the constructor of the class login with three parameters.
  • Call the method validation using the object LG.
  • Using the "LG.EmpNo" public variable we check either user and password correctly.
  • Message box if the information is not correct.
  • If information is correct then we will create the object "alog" of the class UserActivityLog; it calls the constructor with four parameters.
  • Using the "alog" object we call the method task that will insert the data in the database.
  • FrmActivity creates the object of the form.
  • Load the form using the object name frmmain.
Step 14
 
Insert the following code into the FrmActivity form.
 
Learn-OOP-using-Csharp13.jpg 
Learn-OOP-using-Csharp14.jpg
 
Block A
  • We have initialized the position of the form.
  • We create the object "alog" for the class UserActivityLog that will initialize member variables of the class UserActivityLog using three parameters and the values of the parameters are the properties of the login class.
  • The next step is to use a method "task" of the class UserActivityLog with the parameter "Addnew Record".
Block B
  • We have initialized the position of the form.
  • We create an object "alog" for the class UserActivityLog that will initialize the member variables of a class UserActivityLog using three parameters and the values of the parameter are the properties of the login class.
  • The next step is to use the method "task" of the class UserActivityLog with the parameter "Save Record".
The same with the blocks, C, D, and E.
 
Step 15
 
Now press F5. You will see the login screen; just enter:
 
user name: naveed.zaman
user name: khan@123
 
Learn-OOP-using-Csharp15.jpg
 
Click the login button.
 
Learn-OOP-using-Csharp16.jpg
 
Now press the "Add-New" button, "Save" button, "Discard" button, and then press the "Login" button.
 
Step 16
 
Now open then SQL Server table; you will get a result like the following:
 
Learn-OOP-using-Csharp17.jpg