Im a newbie and im working with 4 forms, first form is a main menu which has 6 buttons, if one of the button is clicked, a view only form is shown. this view only form has add edit and delete buttons. if edit or add is clicked, a single form is shown. if add, all fields are blank and if edit, data source is pulled from rows selected. my problem is: I created 1 database in access 07 with multiple tables. atleast 12 tables and every 2 tables are related with each other. all tables are not related with each other but by two's. there is a primary key for each and a foreign key for another. what i want to do is: since i have 6 buttons in form1 which is main form, i want to pull a specific table from my database to fill the form 2. which means, using the same form but with different data filled depending on the button that is clicked from the form1. i hope you can help me find a solution for this. give me an example if i have to do a constructor or a method for this. thank you.

Dani AI

Generated

Short answer for (and building on ’s note): pass an identifier for which table to show into the reused form, then have that form load and bind only that table. A constructor is the simplest for a one-shot open; a public Load method works if the same form instance must be reused. Always map buttons to a whitelist (or enum) of known tables instead of passing raw user input.

Example (constructor + simple load/bind):

public partial class DataForm : Form
{
    readonly string _conn;
    readonly string _tableName;

    public DataForm(string connectionString, string tableName)
    {
        InitializeComponent();
        _conn = connectionString;
        _tableName = tableName;
        LoadTable(_tableName);
    }

    void LoadTable(string tableName)
    {
        var sql = $"SELECT * FROM [{tableName}]";
        using (var conn = new OleDbConnection(_conn))
        using (var da = new OleDbDataAdapter(sql, conn))
        {
            var dt = new DataTable();
            da.Fill(dt);
            bindingSource1.DataSource = dt;
            dataGridView1.DataSource = bindingSource1;
        }
    }
}

Open from the main menu (Form1) by passing the desired table:

private void btnCustomers_Click(object s, EventArgs e)
{
    using (var f = new DataForm(connString, "Customers"))
        f.ShowDialog();
}

private void btnOrders_Click(object s, EventArgs e)
{
    using (var f = new DataForm(connString, "Orders"))
        f.ShowDialog();
}

Notes and pitfalls: use a whitelist/map for button→table to avoid arbitrary table names; bracket table names with spaces; ensure each table has a primary key if updates are needed (OleDbCommandBuilder needs it); for add/edit/delete either use a DataAdapter with CommandBuilder for simple cases or write explicit parameterized INSERT/UPDATE/DELETE for production. If tables have different schemas, either use a generic DataGridView or create small per-table editors that inherit shared behavior from a base form so the UI matches the data.

Recommended Answers

All 2 Replies

can you give me an example of a method I can use on this scenario:

i have ole db with 2 tables not related with each other.

i have connected the db to c# form.

on form1 theres button1 and button2.

i want to:

if button1 is clicked, load form2 or fill with table1 ONLY else load form2 with table2 ONLY.

do I have to use delegate? constructor? method? class?

if you can give me an example please thank you!

>i want to pull a specific table from my database to fill the form 2.

You need to write code to fetch the result in from table. I have sample app for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.