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 6143357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:30:56+00:00 2026-05-23T18:30:56+00:00

I got a problem. I stack! Don’t know if i need a new class

  • 0

I got a problem. I stack!
Don’t know if i need a new class for it!
i want a methode for closing the connection via a Button click.

I already created the constructor:

   public string Server;
   public string Username;
   public string Pwd;
   public string DB;


   MySqlConnection conn;
   string ConnString;

   public DBVerb(string eServer, string eUsername, string ePwd, string eDB)
   {
       this.Server = eServer;
       this.Username = eUsername;
       this.Pwd = ePwd;
       this.DB = eDB;

   }

And this two methods:

        public void Connect(System.Windows.Forms.Label lblStatus)
    {
        try
        {                 
            ConnString = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false",
                                               this.Server, this.Username, this.Pwd, this.DB);
            conn = new MySqlConnection();
            conn.ConnectionString = ConnString;

            if (conn != null)
                conn.Close();


            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                lblStatus.Text = String.Format("Verbindung zu {0} user: {1} Zeit: {2}", this.Server, this.Username, DateTime.Now.ToString());
            }
            else
            {
                MessageBox.Show("Felher");
            }


        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message, "Fehler:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
    public void ClConnect()
    {
        conn = new MySqlConnection();
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }

Here I’m calling the Methode:

       private void cmdHerstellen_Click(object sender, EventArgs e)
    {
        string lServer = txtBServ.Text;
        string lUID = txtBUid.Text;
        string lPawd = txtBPass.Text;
        string lDB = txtBDat.Text;


        DBVerb VerbindungHerstellen = new DBVerb(lServer, lUID, lPawd, lDB);
        VerbindungHerstellen.Err();
        VerbindungHerstellen.Connect(lblStatus);



    }

    private void cmdAbbr_Click(object sender, EventArgs e)
    {


    }

If i call the Method ClConnect() than I have to give the arguments for the parameter, but I already did, so it don’t work.

Any idea how to do it?

  • 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-05-23T18:30:56+00:00Added an answer on May 23, 2026 at 6:30 pm

    You are storing your dbconnection as a field in your class. When you want to close it you don’t want to assign a new connection object to it with conn = new MySqlConnection(); and instead just want to remove that line and replace it with a check to see if conn is null or not. If its null then no work needs to be done (or maybe its an error) and if its not null then you can check if it is open and close if appropriate.

    You probably also want to be careful of where you are creating new objects in the connect method. If conn already exists you probably don’t want to (or need to) create a new connection object.

    My last comment is that there is something wrong sounding about the user needing to click a button to close your connection. That should be soemthign the code worries about and not the user. However, I obviously don’t know what you are doing with this so I can’t say it is definitely wrong, just that it feels a bit wrong. 🙂

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

Sidebar

Related Questions

I got stuck just with one problem: I don't have even an idea why
I got this crazy idea that I don't know is possible or not... what
there's a problem which I don't know how to solve. I know how to
So I got a problem with saving my pdf. I want to create an
I am working on a problem and got stuck at a wall I have
I got stuck in this problem for an hour. I am thinking this is
I've got stuck in a problem with gflags when trying to find some memory
i got problem with my code and hopefully someone able to figure it out.
I've got problem with connecting to MySQL database on my freshly installed Windows 7
Got a problem with ADOMD.NET 8.0, SQL2008 and our app. It isn't giving us

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.