Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:06:44+00:00 2026-06-12T15:06:44+00:00

I am creating a form in c# so any new form inheritances the property

  • 0

I am creating a form in c# so any new form inheritances the property of build in class From
I also created a class called program which starts a connection to my Database I would like all my forms to inherit it because I don’t want to open a new connection for every form
i cant use interface because program contains main() where it creates a connection
My program class is

      class Program
      {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        MySqlConnection myConnection = new MySqlConnection("Server=instance2813231.db.xeround.com;Port=18422;Database=shares info;Uid=user;Pwd=password;");
        MySqlCommand command = myConnection.CreateCommand();

        try
        {

            myConnection.Open();
            MessageBox.Show("connected");


        }
        catch (Exception a)
        {
            Console.WriteLine(a.ToString());
            MessageBox.Show("Failed");
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

How can I get around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:06:46+00:00Added an answer on June 12, 2026 at 3:06 pm

    I would like all my forms to inherit it

    Ick, don’t. It’s an abuse of inheritance in order to achieve code reuse in a very poor way.

    Firstly, try to separate your GUI code from your database code.

    Secondly, favour composition over inheritance – a form isn’t logically a Program or a “database connection opener”. It should use the latter, but it isn’t one. You should have a single entry point, which instantiates whatever it needs to, then starts things running.

    Thirdly, don’t use a single connection through your code – instead, open and close connections as you need to, and let connection pooling take care of the efficiency aspect.

    So instead of deriving from the class, provide each form with a reference to an instance of your database connection opener (if you really want to mix your database and GUI code… ick), and then your form can use that opener.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form for creating a new invoice with many items . class
We created special form to creating purchase prices for vendors. New form has almost
I am working on creating a form which pulls data from a database, and
I am creating one form in html using table. Which have one Drop down
I am creating a form designer. It will generate PHP code which will produce
I created a jtable in gui form in intellij and I dont see any
I have an ajax form for creating a new Category. <%= form_for(@category, :remote =>
I've a form for creating a new Customer . A customer may have a
I'm creating a new blog form and I have a few photo upload fields
I am creating a form, which will send out the details via email upon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.