Dynamically Open Windows Form based on Database value using C# and VB.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

Hi

I want open form from database.

Id

nameForm

1

User    

2

mainForm

	    public DataTable getNameForms()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["depr"].ConnectionString);
            DataTable dt = new DataTable();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "select ltrim(rtrim(nameForm))nameForm from tblPermission where id = 2";
            con.Open();
            try
            {
                dt.Load(cmd.ExecuteReader());
            }
            catch { }
            con.Close();
            return dt;
        }

	    private void btnEnter_Click(object sender, EventArgs e)
        {
	    DataTable d = dep.getNameForms();
            d.Rows[0]["nameForm"] mn = new d.Rows[0]["nameForm"];
	    mn.ShowDialog();
        }

 

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago Modified on 3 months ago

Hi PRA,

Check this example. Now please take its reference and correct your code.

I have three Forms Form1, MainForm and User form.

In Form1 Button click i am opening other two form.

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.CenterScreen;
    }

    public DataTable GetNameForms()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("NameForm");
        dt.Rows.Add(1, "User");
        dt.Rows.Add(2, "MainForm");
        return dt;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt = GetNameForms();
        foreach (DataRow dr in dt.Rows)
        {
            string formName = this.GetType().Namespace + "." + dr["NameForm"].ToString();
            var form = (Form)Activator.CreateInstance(Type.GetType(formName));
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Show();
        }
    }
}

VB.Net

Public Class Form1
    Public Sub New()
        InitializeComponent()
        Me.StartPosition = FormStartPosition.CenterScreen
    End Sub
    Public Function GetNameForms() As DataTable
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Id")
        dt.Columns.Add("NameForm")
        dt.Rows.Add(1, "User")
        dt.Rows.Add(2, "MainForm")
        Return dt
    End Function

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
        Dim dt As DataTable = GetNameForms()
        For Each dr As DataRow In dt.Rows
            Dim formName As String = Me.GetType().Namespace & "." & dr("NameForm").ToString()
            Dim form = CType(Activator.CreateInstance(Type.GetType(formName)), Form)
            form.StartPosition = FormStartPosition.CenterScreen
            form.Show()
        Next
    End Sub
End Class

Screenshot