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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:38:21+00:00 2026-06-14T11:38:21+00:00

I have the following code to populate a dropdownlist: string strConn = ConfigurationManager.ConnectionStrings[PhoQL].ConnectionString; using

  • 0

I have the following code to populate a dropdownlist:

string strConn = ConfigurationManager.ConnectionStrings["PhoQL"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConn))
{
  DataSet ds = new DataSet();
  using (SqlDataAdapter myda = new SqlDataAdapter("SELECT [Abrv], [State] FROM [States]", con))
  {
    myda.Fill(ds)
    ddlShipState.DataSource = ds;
  }
}
ddlShipState.DataTextField = "State";
ddlShipState.DataValueField = "Abrv";
ddlShipState.DataBind();

I was wondering if there is a more efficient way of doing it. Notice I didn’t have to open and close the connection. Wondering if it makes a difference in my example.
In terms of more efficient I am looking for best code practice for what I have above.

  • 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-14T11:38:23+00:00Added an answer on June 14, 2026 at 11:38 am

    Notice I didn’t have to open and close the connection.

    No, but the DataAdapter does it implicitely.

    MSDN:

    If the IDbConnection is closed before Fill is called, it is opened to
    retrieve data and then closed. If the connection is open before Fill
    is called, it remains open.

    But also note that opening and closing is not inefficient when you’re using Connection-Pooling(default). Because then con.Open just means “wait, i need this connection now” and con.Close means “ok, you can reuse it somewhere else now, i’m finished”.

    So you should always close a connection as soon as you’re finished with it. Otherwise the connection-pool needs to open new physical connections everytime which can cause exceptions(maximum default connection count is 100) but always will decrease the performance.

    The best way to dispose/close connections(any IDisposable) is by using the using statement:

    using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["PhoQL"].ConnectionString))
    using(var cmd = new SqlCommand("SELECT [Abrv], [State] FROM [States]", con))
    using(var da = new SqlDataAdapter(cmd))
    {
        DataTable tbl = new DataTable();
        da.Fill(tbl);
        ddlShipState.DataSource = tbl;
        ddlShipState.DataTextField = "State";
        ddlShipState.DataValueField = "Abrv";
        ddlShipState.DataBind();
    } 
    

    (sidenote: i’ve replaced your DataSet with a “lighter” DataTable)

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

Sidebar

Related Questions

I have the following code to populate a DropDownList var list = new Dictionary<string,
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
So I have the following (pseudo code): string selectedvalud = C; List<SelectListItem> list= new
I have the following code I am using to populate a second dropdown box
I have the following code which I'm using to populate form fields. This code
I have the following code to populate an array of strings, but every time
I have following code for inserting data into database using PDO. It inserts data
I have following code using hibernate to throw a custom exception on error and
I have following code in my MVC2 view: <tr class=edit style=display:none> <td> <%= Html.DropDownList(drpFields,
I have following code in my MVC2 view: <tr class=edit style=display:none> <td> <%= Html.DropDownList(drpFields,

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.