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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:34:29+00:00 2026-05-20T10:34:29+00:00

Noob ADO.NET question: Can I do the following? Retrieve a DataTable somehow. Dispose it.

  • 0

Noob ADO.NET question: Can I do the following?

  1. Retrieve a DataTable somehow.
  2. Dispose it.
  3. Still use its data. (But not send it back to the database, or request the database to update it.)

I have the following function, which is indirectly called by every WebMethod in a Web Service of mine:

public static DataTable GetDataTable(string cmdText, SqlParameter[] parameters)
{
    // Read the connection string from the web.config file.
    Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/WSProveedores");
    ConnectionStringSettings connectionString = configuration.ConnectionStrings.ConnectionStrings["..."];

    SqlConnection connection = null;
    SqlCommand command = null;
    SqlParameterCollection parameterCollection = null;
    SqlDataAdapter dataAdapter = null;
    DataTable dataTable = null;

    try
    {
        // Open a connection to the database.
        connection = new SqlConnection(connectionString.ConnectionString);
        connection.Open();

        // Specify the stored procedure call and its parameters.
        command = new SqlCommand(cmdText, connection);
        command.CommandType = CommandType.StoredProcedure;
        parameterCollection = command.Parameters;
        foreach (SqlParameter parameter in parameters)
            parameterCollection.Add(parameter);

        // Execute the stored procedure and retrieve the results in a table.
        dataAdapter = new SqlDataAdapter(command);
        dataTable = new DataTable();
        dataAdapter.Fill(dataTable);
    }
    finally
    {
        if (connection != null)
        {
            if (command != null)
            {
                if (dataAdapter != null)
                {
                    // Here the DataTable gets disposed.
                    if (dataTable != null)
                        dataTable.Dispose();
                    dataAdapter.Dispose();
                }

                parameterCollection.Clear();
                command.Dispose();
            }

            if (connection.State != ConnectionState.Closed)
                connection.Close();
            connection.Dispose();
        }
    }

    // However, I still return the DataTable
    // as if nothing had happened.
    return dataTable;
}
  • 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-20T10:34:30+00:00Added an answer on May 20, 2026 at 10:34 am

    The general rule is to Dispose anything that implements IDisposable, whether it “needs it” or not. Saves you from the times when it “needs it” and you didn’t think to Dispose.

    Once you’ve called Dispose on an object, you shouldn’t use it anymore. Period. It violates the entire concept of Dispose.

    This is why you shouldn’t call Dispose on an object that you are returning from a method. Leave it up to the caller to call Dispose when they’re done with the object.


    BTW, your code could be simpler. More like this:

    using (SqlConnection connection = new SqlConnection(connectionString.ConnectionString))
    {
        connection.Open();
    
        using (SqlCommand command = new SqlCommand(cmdText, connection))
        {
            //...
            using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
            {
                DataTable dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
                return dataTable;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this may be a noob question, but it's bugging the heck out
Just some background, sorry so long winded. I'm using the System.Data.SQLite ADO.net adapter to
Noob question: Consider the following C# code: public IEnumerable<xpto> CalculatedList { get { foreach(var
This is a noob question, but I will ask it anyway... I'm wanting to
This might be a noob question, but I need help. I screwed up my
Noob-ish question: I'm looking for a lightweight but decent php way to search all
This is a noob question I know, but is it possible to really write
This is going to be the noobist of all noob questions, but what exactly
I've got a pretty noob question for ya. I keep seeing this syntax: <%=
Noob question: I'm currently under the impression that when you want to create an

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.