Rose Rouge

Rose Rouge

  • NA
  • 27
  • 8.7k

Hide some menu from MenuStrip Winfoms

Jun 20 2016 5:18 AM

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 :
  1. public Visite(string username)  
  2.     {  
  3.         InitializeComponent();  
  4.         label1.Text = username;  
  5.   
  6.         using (SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True"))  
  7.         {  
  8.             con.Open();  
  9.             bool UserIsAdmin = true;  
  10.             using (SqlCommand cmd = new SqlCommand("select * from [User] where Role =@Role", con))  
  11.             {  
  12.                 cmd.Parameters.AddWithValue("@Role""Admin");  
  13.   
  14.                 UserIsAdmin = (int)cmd.ExecuteScalar() > 0;  
  15.             }  
  16.             if (UserIsAdmin == false)  
  17.             {  
  18.                 utilisateurToolStripMenuItem.Visible = false;  
  19.             }  
  20.            else  
  21.            {  
  22.                utilisateurToolStripMenuItem.Visible= true;  
  23.             }  
  24.            con.Close();  
  25.        }  
  26.   
  27.     }  
 
Can someBody Have any idea ,please  ??

Answers (7)