Billing System
11. Displaying the username of logged in user in Admin or User Dashboard
In the previous episode, we have successfully added the login functionality on our "BILLING AND INVENTORY MANAGEMENT SYSTEM".
Now, based on that login system, we will display the username of the logged in user on our Admin or User Dashboard. And we will also insert the id of logged in user on added_by field while creating new user.
For this, we will also create a new method on userDAL.cs to get the id of logged in user from username.
WATCH THE VIDEO FIRST
Displaying the username of user on Admin and User Dashboard
Step 1: Creating Static String Method
In order to display the username of the user who is logged in first we need to create a public static string type method on frmLogin.cs as follows
public static string loggedIn;
Step 2: Assigning the Value on Static Method
After that, we need to assign the value on this string method when the user is logged in successfully. The code to assign the value on this method is as follows:
loggedIn = l.username;
Note: paste this code after displaying the login success method on button click event on frmLogin.cs.
Step 3: Calling this static method on Admin and User Dashboard
After the value is assigned to the static method, now we need to call this method to our User and Admin Dashboard where we want to display the username.
Firstly, Create the label on Admin and User Dashboard both.
Secondly, Create form load event and assign the value from static method of login form to this label as follows
[Do this for both Admin and User Dashboard]
[Do this for both Admin and User Dashboard]
lblLoggedInUser.Text = frmLogin.loggedIn;
Now, when you successfully login to our "BILLING AND INVENTORY MANAGEMENT SYSTEM", it should display the username of logged in user on Admin as well as User Dashboard.
If you have any confusion then WATCH THE VIDEO AGAIN, else move to the next step.
Method to Get the id or user who is logged in
Step 1: Creating the method to get the id of user from username
Please paste the following code on userDAl.cs to create the Method to get the id o logged in user
public userBLL GetIDFromUsername (string username)
{
userBLL u = new userBLL();
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();
try
{
string sql = "SELECT id FROM tbl_users WHERE username='"+username+"'";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
conn.Open();
adapter.Fill(dt);
if(dt.Rows.Count>0)
{
u.id = int.Parse(dt.Rows[0]["id"].ToString());
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return u;
}
Step 2: Using the method to get the id of user who is logged in
After the method is created successfully, we need to use this method to get the id of user on frmUser.cs.
Please paste the following code on add button click event while getting the data from form, to get the id from user
//Getting Username of the logged in user
string loggedUser = frmLogin.loggedIn;
userBLL usr = dal.GetIDFromUsername(loggedUser);
u.added_by = usr.id;
The code above will get the id of logged in user and set on added_by field while creating new user.
In the next episode, we will start Category Module which will be used to add new category, update existing category and delete the category.
Thank you so much for reading this post.
If you are interested to join this course please click on the link below
Post a Comment
0 Comments