Open Windows Form based on Database record on ToolStripMenuItem Click using C# and VB.Net

Last Reply 8 days ago By pandeyism

Posted 10 days ago

Hi! I have table in sql with form name.

Id nameForm
1 mainForm
2 regForm
3 docForm

Into form I have menu with submenu. When I click submenu then check form name with table name form if exist and equal then show form else show message you haven't access.

namespace FormsApp
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cash"].ConnectionString);
        DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }
 
        public void getFormName(string name)
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "select nameForm accesTable where nameForm = '" + name + "'";
            con.Open();
            try
            {
                cmd.ExecuteReader();
            }
            catch { }
            con.Close();
        }
 
        private void mainFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var form = (Form)Activator.CreateInstance(Type.GetType("mainForm"));
            dt = getFormName(form.ToString());
            if (dt.Rows.Count > 0)
                form.ShowDialog();
            else
                MessageBox.Show("You haven't access to this form!");
        }
    }
}

 

You are viewing reply posted by: pandeyism 8 days ago.
Posted 8 days ago

Hey PRA,

Please refer below smaple.

Use dropdownItemClicked Event.

Namespaces

C#

using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient

Code

C#

private void mainMenuToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    string submenu = e.ClickedItem.Text;
    string constr = @"Server=.\SQL2005;DataBase=Test;UID=sa;PWD=pass@123";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Title FROM Menus WHERE Title=@Title", con))
        {
            cmd.Parameters.AddWithValue("@Title", submenu);
            con.Open();
            string menuItemName = Convert.ToString(cmd.ExecuteScalar());

            if (menuItemName == submenu)
            {
                Form2 f2 = new Form2();
                f2.ShowDialog();
            }
            else
            {
                MessageBox.Show("You haven't access to this form!");
            }
            con.Close();
        }
    }
}

VB.Net

Private Sub MainMenuToolStripMenuItem_DropDownItemClicked_1(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MainMenuToolStripMenuItem.DropDownItemClicked
    Dim submenu As String = e.ClickedItem.Text
    Dim constr As String = "Server=.\SQL2005;DataBase=Test;UID=sa;PWD=pass@123"
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT Title FROM Menus WHERE Title=@Title", con)
            cmd.Parameters.AddWithValue("@Title", submenu)
            con.Open()
            Dim menuItemName As String = Convert.ToString(cmd.ExecuteScalar())
            If menuItemName = submenu Then
                Dim f2 As Form2 = New Form2()
                f2.ShowDialog()
            Else
                MessageBox.Show("You haven't access to this form!")
            End If
            con.Close()
        End Using
    End Using
End Sub

Screenshot