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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:40:10+00:00 2026-05-25T06:40:10+00:00

I am populating a DropDownList from a SQL Server database as shown below. It

  • 0

I am populating a DropDownList from a SQL Server database as shown below. It works fine, but I’m not sure it’s a good way. Can someone shed some light on this method, and give some improvements?

private void LoadSubjects()
{
    ddlSubjects.Items.Clear();
    string selectSQL = "SELECT SubjectID,SubjectName FROM Students.dbo.Subjects";

    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataReader reader;

    try
    {
        ListItem newItem = new ListItem();
        newItem.Text = "<Select Subject>";
        newItem.Value = "0";
        ddlSubjects.Items.Add(newItem);

        con.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            newItem = new ListItem();
            newItem.Text = reader["SubjectName"].ToString();
            newItem.Value = reader["SubjectID"].ToString();
            ddlSubjects.Items.Add(newItem);
        }
        reader.Close();
    }
    catch (Exception err)
    {
        //TODO
    }
    finally
    {
        con.Close();
    }
}
  • 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-25T06:40:11+00:00Added an answer on May 25, 2026 at 6:40 am

    You could bind the DropDownList to a data source (DataTable, List, DataSet, SqlDataSource, etc).

    For example, if you wanted to use a DataTable:

    ddlSubject.DataSource = subjectsTable;
    ddlSubject.DataTextField = "SubjectNamne";
    ddlSubject.DataValueField = "SubjectID";
    ddlSubject.DataBind();
    

    EDIT – More complete example

    private void LoadSubjects()
    {
    
        DataTable subjects = new DataTable();
    
        using (SqlConnection con = new SqlConnection(connectionString))
        {
    
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT SubjectID, SubjectName FROM Students.dbo.Subjects", con);
                adapter.Fill(subjects);
    
                ddlSubject.DataSource = subjects;
                ddlSubject.DataTextField = "SubjectNamne";
                ddlSubject.DataValueField = "SubjectID";
                ddlSubject.DataBind();
            }
            catch (Exception ex)
            {
                // Handle the error
            }
    
        }
    
        // Add the initial item - you can add this even if the options from the
        // db were not successfully loaded
        ddlSubject.Items.Insert(0, new ListItem("<Select Subject>", "0"));
    
    }
    

    To set an initial value via the markup, rather than code-behind, specify the option(s) and set the AppendDataBoundItems attribute to true:

    <asp:DropDownList ID="ddlSubject" runat="server" AppendDataBoundItems="true">
        <asp:ListItem Text="<Select Subject>" Value="0" />
    </asp:DropDownList>
    

    You could then bind the DropDownList to a DataSource in the code-behind (just remember to remove:

    ddlSubject.Items.Insert(0, new ListItem("<Select Subject>", "0"));
    

    from the code-behind, or you’ll have two “” items.

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

Sidebar

Related Questions

i have a dropdownlist that populates from the sql server database. populating the list
This is a followup from Populating DropDownList inside Repeater not working . I'm adding
I'm populating a dropdownlist in c# asp.net-MVC from a SQL table using Linq2Sql. I'd
In my application I'am populating a dropdownlist from database using ADO Entity Framework, after
I have a very basic syntax question on Populating a dropdownlist box from dataset.
Im populating a GridView from List so am forced to use TemplateField controls to
I am populating a DataGridView control on a Windows Form (C# 2.0 not WPF).
I'm populating a ListView with items and add images from an ImageList (in the
I am populating a combobox dropdown with node fields from an Xml document. I
I have a DropDownList on an ASP.NET page that gets populated by a SQL

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.