I have a MenuStrip in my Windows Form Application which Contains MenuItems.
I want to check some condition and disable the visibility of MenuItems in the MenuStrip bar. Suppose Normal User is accessing the application ,then Some MenuItems will be hidden and if Admin user is accessing the application then all MenuItems should be visible.
I try This code but it doesn't work :
- public Visite(string username)
- {
- InitializeComponent();
- label1.Text = username;
-
- using (SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True"))
- {
- con.Open();
- bool UserIsAdmin = true;
- using (SqlCommand cmd = new SqlCommand("select * from [User] where Role =@Role", con))
- {
- cmd.Parameters.AddWithValue("@Role", "Admin");
-
- UserIsAdmin = (int)cmd.ExecuteScalar() > 0;
- }
- if (UserIsAdmin == false)
- {
- utilisateurToolStripMenuItem.Visible = false;
- }
- else
- {
- utilisateurToolStripMenuItem.Visible= true;
- }
- con.Close();
- }
-
- }
Can someBody Have any idea ,please ??