I want to create a Login form which redirects the user login to the two different form according to the roles of users. I have two forms 1. UserPanelFrm and 2.FrmUserRole and two user role . 1. Admin and 2.User . I want to redirect Admin to UserPanelFrm and User to form FrmUserRole. I researched for this process but only could found useful resources for ASP.NET.
tbl_Staff:
CREATE TABLE [dbo].[tbl_Staff](
[StaffID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Address] [nvarchar](500) NULL,
[Phone] [nvarchar](100) NULL,
[Email] [nvarchar](100) NULL,
[JoinedDate] [date] NULL,
[Username] [nvarchar](50) NULL,
[Password] [nvarchar](max) NULL,
[CreatedDate] [date] NULL,
[Roles] [nvarchar](200) NULL,
[Status] [int] NULL
}
tbl_StaffRoles:
CREATE TABLE [dbo].[tbl_StaffRoles](
[id] [int] NULL,
[RoleDescription] [nvarchar](50) NULL
)
tbl_StaffRoles data:
id RoleDescription
1 Admin
2 User
Hi, I am currently using following code for normal login.
LoginForm btnLogin :
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
int result = uc.Login(txtUserName.Text, txtPassword.Text);
if (result == 1)
{
this.Hide();
UserPanelFrm frm = new UserPanelFrm();
frm.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("INVALID USERNAME OR PASSWORD");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
UserClass.cs Login class:
public int Login(String Username, String Password)
{
try
{
int result = 0;
SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
conn.Close();
if (dt.Rows.Count > 0)
result = 1;
else
result = 0;
return result;
}
catch (Exception ex)
{
throw ex;
}
}
Please help to modify the code so i can redirect users to windows form according to their roles.
Modify the Login method to return the datatable instead (assuming you have a column for the role in that query):
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
DataTable result = uc.Login(txtUserName.Text, txtPassword.Text);
if (result.Rows.Count == 1)
{
this.Hide();
string role = result.Rows[0]["Role"].ToString();
switch (role)
{
case "User":
UserPanelFrm frm = new UserPanelFrm();
frm.ShowDialog();
this.Close();
break;
case "Admin":
//Show a different form
FrmUserRole fur = new FrmUserRole();
fur.ShowDialog();
this.Close();
break;
}
}
else
{
MessageBox.Show("INVALID USERNAME OR PASSWORD");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and...
public DataTable Login(String Username, String Password)
{
try
{
SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
conn.Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}