I restricted some menus in the window forms I created with menustrip for users. The restrictions I set manually work just fine. But I want to put and remove the constraints using checkboxes in the form

instead of putting them manually. But my codes cannot pull constraints from database. I am using access database. I will be glad if you help.
if I do like this it works :
C#
if (yetkim == "1")
{
sabitBilgilerToolStripMenuItem.Enabled = true;
kitapIslemleriToolStripMenuItem.Enabled=treu;
üyeIslemleriToolStripMenuItem.Enabled = true;
kitapAlisVerisiToolStripMenuItem.Enabled = true;
raporlarToolStripMenuItem.Enabled = true;
ayarlarToolStripMenuItem.Enabled = true;
güvenliÇikisToolStripMenuItem.Enabled = true;
}
else
{
sabitBilgilerToolStripMenuItem.Enabled = false;
kitapIslemleriToolStripMenuItem.Enabled = false;
üyeIslemleriToolStripMenuItem.Enabled = false;
kitapAlisVerisiToolStripMenuItem.Enabled = true;
raporlarToolStripMenuItem.Enabled = false;
ayarlarToolStripMenuItem.Enabled = false;
güvenliÇikisToolStripMenuItem.Enabled = true;
kitapIslemleriToolStripMenuItem.Enabled = true;but this doesn't work :
C#
if (yetkim == "1")
{
sabitBilgilerToolStripMenuItem.Enabled = true;
kitapIslemleriToolStripMenuItem.Enabled = true;
üyeIslemleriToolStripMenuItem.Enabled = true;
kitapAlisVerisiToolStripMenuItem.Enabled = true;
raporlarToolStripMenuItem.Enabled = true;
ayarlarToolStripMenuItem.Enabled = true;
güvenliÇikisToolStripMenuItem.Enabled = true;
}
else
{
OleDbDataAdapter adap = new OleDbDataAdapter("SELECT akatad FROM altkatagori", conn);
DataTable tbl = new DataTable();
adap.Fill(tbl);
}

Tuhin PaulRank #7 · 71.9K reputationPosted Mar 21, 2023, 9:33 PM EDT
if (yetkim == "1") { // Enable all menu items foreach (ToolStripMenuItem item in menuStrip1.Items) { item.Enabled = true; } } else { // Retrieve the list of restricted menu items from the database List restrictedItems = new List(); using (OleDbConnection conn = new OleDbConnection("your connection string")) { conn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT akatad FROM altkatagori", conn); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { restrictedItems.Add(reader["akatad"].ToString()); } reader.Close(); conn.Close(); } // Disable the restricted menu items foreach (ToolStripMenuItem item in menuStrip1.Items) { if (restrictedItems.Contains(item.Text)) { item.Enabled = false; } else { item.Enabled = true; } } }you can modify your code like this. The menu items must have the same name as the values in the "akatad" column in the "altkatagori" table for this code to work.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 21, 2023, 9:32 PM EDT
Hello Mehmet ,
In your second code snippet, you are only executing a database query to retrieve data from the "altkatagori" table, but you are not setting the enabled/disabled state of the menu items based on the result of the query.
Mehmet FatihRank #1,474 · 1.1K reputationPosted Mar 22, 2023, 5:49 AM EDT
Thank you very much for your attention Tuhin. The problem was solved.