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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:36:00+00:00 2026-05-25T16:36:00+00:00

I’m new to asp.net so this might be really basic question, but i cant

  • 0

I’m new to asp.net so this might be really basic question, but i cant figure it out.

I found a bit of code on the internet, that connects to database. And i created a namespace and some classes to use the same code in different projects.

The code and my class is the following:

namespace databaseFunctions
{
    public class databaseConnection
    {
private static string databaseConnectionString()
        {
            return "DRIVER={MySQL ODBC 5.1 Driver}; ........";
        }

        public static DataTable getFromDatabase(string SQL)
        {
            DataTable rt = new DataTable();
            DataSet ds = new DataSet();
            OdbcDataAdapter da = new OdbcDataAdapter();
            OdbcConnection con = new OdbcConnection(databaseConnectionString());
            OdbcCommand cmd = new OdbcCommand(SQL, con);
            da.SelectCommand = cmd;
            da.Fill(ds);
            try
            {
                rt = ds.Tables[0];
            }
            catch
            {   
                rt = null;
            }
            return rt;
        }

        public static Boolean insertIntoDatabase(string SQL)
        {

            OdbcDataAdapter da = new OdbcDataAdapter();
            OdbcConnection con = new OdbcConnection(databaseConnectionString());
            OdbcCommand cmd = new OdbcCommand(SQL, con);
            con.Open();
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }

        }

}

There is no problem getting data from database, or insert data into some database.
But. when i try to get the last_insert_id() from the mysql database. i only get a zero.

This is why i think that this piece of code I’ve created and copied from internet, creates a new connection for every time i call the “getFromDatabase(SQL)”

Is there anyone that could help me with fixing this class getFromDatabase() to keep the databaseconnection alive until i tell the program to abandon the connection?

I guess it is the “new OdbcConnection” that should be changed? Is it possible to check if there already is a connection alive?
I’ve done this hundreds of times in classic asp, but now, with classes and stuff. I’m totally lost.

  • 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-25T16:36:00+00:00Added an answer on May 25, 2026 at 4:36 pm

    The problem you face is that you’ve coded yourself into a “new connection per action” corner. What you really want to aim for,and is considered best practice, is “new connection per batch of actions”.

    What I recommend in this case is to open connection when required, and close when disposed. What we’ll do is move the odbc adapters to a larger scoped variable so that it can be accessed within the class.

    namespace databaseFunctions
    {
        public class databaseConnection:IDisposable
        {
            private OdbcConnection con;
            private string connectionString;
    
            public databaseConnection(string connectionString){
                this.connectionString = connectionString;
            }
    
    
            public void OpenConnection(){
                if (con == null || con.IsClosed ){ // we make sure we're only opening connection once.
                    con = new OdbcConnection(this.connectionString);
                }
            }
            public void CloseConnection(){
                if (con != null && con.IsOpen){ // I'm making stuff up here
                    con.Close();
                }
            }
    
            public DataTable getFromDatabase(string SQL)
            {
                OpenConnection();
    
                DataTable rt = new DataTable();
                DataSet ds = new DataSet();
                OdbcCommand cmd = new OdbcCommand(SQL, con);
                da.SelectCommand = cmd;
                da.Fill(ds);
                try
                {
                    rt = ds.Tables[0];
                }
                catch
                {   
                    rt = null;
                }
                return rt;
            }
    
            public Boolean insertIntoDatabase(string SQL)
            {
                OpenConnection();
    
                OdbcCommand cmd = new OdbcCommand(SQL, con);
                con.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    return false;
                }
    
            }
    
    
            // Implementing IDisposable method
            public void Dispose(){
                CloseConenction();
            }
        }
    }
    

    Now the next time you use your class do something like

    using (DatabaseConnection db = new DatabaseConnection()){
        db.InsertIntoDatabase(...);
        db.GetLastInsertID();
        db.GetFromDatabase(...);
    }
    

    At the end of that code block, because it is IDisposeable, it will close that connection for you in the dispose method.


    Things I changed:

    • implemented IDisposable interface
    • changed methods from static to class methods.
    • added new methods for opening closing connection
    • moved connection variable to class level scope
    • added an argument to the constructor that lets you pass in a connection string (you should put this connection string in you Web.Config

    Edits:

    • constructor takes in connectionString per suggestion.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.