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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:38:31+00:00 2026-05-30T10:38:31+00:00

I have a class containing methods to fill DropDowns, return DataSet, return Scalar or

  • 0

I have a class containing methods to fill DropDowns, return DataSet, return Scalar or simply excute a query. In one of my older posts in StackOverflow, I submitted a buggy code of the same class. Based on the advice of the contributors, I have improved the code and want to know whether this class is suitable to be used in a high-concurrent environment:

public sealed class reuse
{
    public void FillDropDownList(string Query, DropDownList DropDownName)
    {
        using (TransactionScope transactionScope = new TransactionScope())
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString.ToString()))
            {
                SqlDataReader dr;
                try
                {
                    if (DropDownName.Items.Count > 0)
                        DropDownName.Items.Clear();

                    SqlCommand cmd = new SqlCommand(Query, con);
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                        DropDownName.Items.Add(dr[0].ToString());

                    dr.Close();
                }
                catch (Exception ex)
                {
                    CustomErrorHandler.GetScript(HttpContext.Current.Response,ex.Message.ToString());
                }
            }
        }
    }
}

I want to know whether to dispose Command and DataReader objects as well or they too will get automatically disposed with USING?

  • 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-30T10:38:33+00:00Added an answer on May 30, 2026 at 10:38 am

    The the command/reader: they would be disposed by “using”, but only if you use “using” for them, which you should.

    Criticisms:

    • you are mixing UI and data access horribly – the exception handling in particular gives no indication to the calling code (although personally I’d keep the control code separate too), and assumes the caller always wants that script-based approach (to me, if this code fails, things are very wrong: let that exception bubble upwards!)
    • no mechanism for proper parameters; my suspicion then, is that you’re concatenating strings to make a query – potential (but very real) risk of SQL injection
    • you mention high-concurrent; if so, I would expect to see some cache involvement here
    • for code maintenance reasons, I’d move all “create a connection” code to a central point – “DRY” etc; I wouldn’t expect an individual method like this to concern itself with details like where the connection-string comes from

    Frankly I’d just use dapper here, and avoid all these issues:

    using(var connection = Config.OpenConnection()) {
         return connection.Query<string>(tsql, args).ToString();
    }
    

    (and let the caller iterate over the list, or use AddRange, or data-binding, whatever)

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

Sidebar

Related Questions

I have a class where one of the public methods is a perfect fit
We have an entity (class) containing data and methods (lets call it Person). There
I have a .NET class library containing a class with a method that performs
I have a class containing Linq To SQL objects that are used to populate
I have a domain class containing a couple of fields. I can access them
I have a class library containing several structures each consisting of several value and
I have an example class containing two data points: public enum Sort { First,
I have a class A containing two pointers to objects of another class B.
I have a Shape class containing potentially many vertices, and I was contemplating making
I have a java class containing all the columns of a database table as

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.