In my first test project the value of table column “ROLE” for the role of users were as follows: “A” for Admin and “U” for User.
I actually changed from “A” to “Admin” and from “U” to “User” because I don’t want only single letter alphabet to display on the gridview.
This value were inserted into table column Role as shown below:
First Table that all page controls were hidden
Second Table that all web page control were visible
Then when a user logs in and is being redirected to the homepage, there are some controls on the homepage that are supposed to be hidden based on the user who logged in, if it is user “A” who logged in then all controls will be visible but if it is user “U” some control will be hidden. After, I changed the values of the Role column in the table to “Admin” and “User” and when I log in all controls is visible even if it is “User” who logged in.
C# on form Load event:
public partial class Home : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("LoginForm.aspx");
}
else
{
showdata1();
showdata();
user.Visible = true;
named.Visible = true;
showdata2();
}
}
public void showdata()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM UserWallet AS w INNER JOIN Users AS u ON w.email = u.CreatedBy WHERE u.Uid = '" + Session["user"] + "'";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = cmd;
sda.Fill(ds, "detail");
if (ds.Tables[0].Rows.Count > 0)
{
named.Text = ds.Tables[0].Rows[0]["Name"].ToString();
Units.Text = ds.Tables[0].Rows[0]["amount"].ToString();
}
}
public void showdata2()
{
cmd.CommandText = "select * from Users where Uid= '" + Session["user"] + "'";
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(ds, "detail");
if (ds.Tables[0].Rows.Count > 0)
{
string Role;
Role = ds.Tables[0].Rows[0][4].ToString().Trim().ToUpper();
if (Role == "User")
{
dashboard.Visible = false;
}
else if (Role == "Admin")
{
dashboard.Visible = true;
}
}
}
}