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

  • Home
  • SEARCH
  • 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 8961097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:31+00:00 2026-06-15T15:48:31+00:00

I am populating DDL’s in asp.net web forms and use a sqlutilility class that

  • 0

I am populating DDL’s in asp.net web forms and use a sqlutilility class that I have for all my sql classes that call my stored procedures.

But on each drop down list I keep repeating the same code and renameing it and then calling it when I want to use it.

What I want to know is. is there a way using this code instead of repeating it all the time?

    protected void Populate_Authority()
    {
    //  Init()
    //  ------
    SqlConnection conn = null;
    SqlDataReader rdr = null;
    SqlCommand cmd = null;
    //  
    drp_Authority.Items.Clear();

    try
    {
        //  Conn
        //  ----
        conn = new      SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        conn.Open();

        //  Cmd
        //  ---
        cmd = new SqlCommand("[sp_Populate_Authority]", conn);
        cmd.CommandType = CommandType.StoredProcedure;


        //  Execute
        //  -------
        rdr = cmd.ExecuteReader();

        //  Row(s)?
        //  -------
        if (rdr.HasRows)
        {
            //  Read
            //  ----
            while (rdr.Read())
            {
                //  Populate
                //  --------
                drp_Authority.Items.Add(
                    new ListItem(
                        rdr["Authority"].ToString(),
                        rdr["AuthorityID"].ToString()));
            }

            //  Blank
            //  -----
            drp_Authority.Items.Insert(0, String.Empty);
        }

        //  Clean up / Close down
        //  ---------------------
        cmd.Dispose();
        rdr.Dispose();
        rdr.Close();
        conn.Dispose();
        conn.Close();

    }
    catch (SqlException ex)
    {
        //  Throw Sql Exception 
        //  -------------------
        throw ex;
    }
    finally
    {
        //  Clean up / Close down
        //  ---------------------
        if (cmd != null)
        {
            cmd.Dispose();
        }
        if ((rdr != null) && (!rdr.IsClosed))
        {
            rdr.Dispose();
            rdr.Close();
        }
        if ((conn != null) && (conn.State != ConnectionState.Closed))
        {
            conn.Dispose();
            conn.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-06-15T15:48:33+00:00Added an answer on June 15, 2026 at 3:48 pm

    Take your code, wrap it in a helper method:

    protected void Populate_Ddl(DropDownList ddl, string command, string valueName, string keyName)
        {
        //  Init()
        //  ------
        SqlConnection conn = null;
        SqlDataReader rdr = null;
        SqlCommand cmd = null;
        //  
        ddl.Items.Clear();
    
        try
        {
            //  Conn
            //  ----
            conn = new      SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            conn.Open();
    
            //  Cmd
            //  ---
            cmd = new SqlCommand(command, conn);
            cmd.CommandType = CommandType.StoredProcedure;
    
    
            //  Execute
            //  -------
            rdr = cmd.ExecuteReader();
    
            //  Row(s)?
            //  -------
            if (rdr.HasRows)
            {
                //  Read
                //  ----
                while (rdr.Read())
                {
                    //  Populate
                    //  --------
                    ddl.Items.Add(
                        new ListItem(
                            rdr[keyName].ToString(),
                            rdr[valueName].ToString()));
                }
    
                //  Blank
                //  -----
                ddl.Items.Insert(0, String.Empty);
            }
    
            //  Clean up / Close down
            //  ---------------------
            cmd.Dispose();
            rdr.Dispose();
            rdr.Close();
            conn.Dispose();
            conn.Close();
    
        }
        catch (SqlException ex)
        {
            //  Throw Sql Exception 
            //  -------------------
            throw ex;
        }
        finally
        {
            //  Clean up / Close down
            //  ---------------------
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if ((rdr != null) && (!rdr.IsClosed))
            {
                rdr.Dispose();
                rdr.Close();
            }
            if ((conn != null) && (conn.State != ConnectionState.Closed))
            {
                conn.Dispose();
                conn.Close();
            }
        }
    }
    

    Then call it from your code:

    Populate_Ddl(drp_Authority, "[sp_Populate_Authority]", "Authority", "AuthorityID");
    

    And repeat for all ddl’s.

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

Sidebar

Related Questions

I have my GridView populating like so: <asp:Panel ID=pnlGrid runat=server> <div class=m_container style=margin-bottom:20px;> <asp:GridView
I'm populating a dropdownlist in c# asp.net-MVC from a SQL table using Linq2Sql. I'd
I am populating a class using Linq to SQL. What I am trying to
When populating my treeview I would like to use the same images that I
We have a cube that we are populating the data from the source tables.
I am populating an XML structure (using VB.NET) to pass to a web service.
Im populating a GridView from List so am forced to use TemplateField controls to
I'm populating a CheckBoxList from database table. As per requirement, all the values should
We have accounting software populating the name field on a magento webstore, the problem
I have TreeView Populating from Database And I am Trying to detect the user

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.