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

The Archive Base Latest Questions

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

I am currently working on an asp.net web page with a GridView displaying a

  • 0

I am currently working on an asp.net web page with a GridView displaying a table from a database. This GridView has 4 DropDownLists that will be used to filter the data shown on the GridView. When the page loads 4 Sub routines are run, each one connecting to the database with a select statement to fill the DropDownList with relevant filter headings.

Initially, I had one connection with a loop that populated all of the drop downs but these contained duplicates. I then split the filling of each DDL so that the select statements could contain DISTINCT.

I would like (and am sure there is a way here) to be able to populate all of the DDLs with data from one connection.

Code for one connection:

        Protected Sub FillDepDDL()

        Dim conn As New SqlConnection()
        conn.ConnectionString = WebConfigurationManager.ConnectionStrings("TestDBConnectionString").ConnectionString

        Dim connection As New SqlConnection(conn.ConnectionString)
        connection.Open()

        Const FillAllQS As String = "SELECT DISTINCT [Department] FROM [Employees]"

        Dim command As New SqlCommand(FillAllQS, connection)

        Dim reader As SqlDataReader = command.ExecuteReader()

        Dim sel As New ListItem
        sel.Text = "Please Select"
        sel.Value = "*"
        DDLDepartment.Items.Add(sel)
        While reader.Read

            Dim Deplist As New ListItem()
            Deplist.Value = reader("Department")
            Deplist.Text = reader("Department")
            DDLDepartment.Items.Add(Deplist)

        End While
        reader.Close()
        conn.Close()

    End Sub

The other 3 column names: FirstName > DDLFN, LastName > DDLLN, Wage > DDLWag.

This is only a test DB and the princibles learned here will be applied to a larger live project.

I’m sure some guru will be able to work this out easily but I just can’t get my head round it even after hours of searching.

Thanks in advance.

  • 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-29T10:03:40+00:00Added an answer on May 29, 2026 at 10:03 am

    I’m adding this in as answer because I cannot format it in a comment, but this doesn’t answer the original question of how to write the sql to return all three distinct result sets. Instead, it answers how to rewrite the code you have above so that connections are properly disposed of in case of an exception.

    Protected Sub FillDepDDL()
          Dim Deplist As ListItem
          Dim sel As New ListItem
          sel.Text = "Please Select"
          sel.Value = "*"
          DDLDepartment.Items.Add(sel)  
    
          Using conn As New SqlConnection(WebConfigurationManager.ConnecitonString("TestDBConnectionString").ConnectionString)          
               Using cmd As New SqlCommand("SELECT DISTINCT [Department] FROM [Employees]", conn)
                    conn.Open()
    
                    Using reader = cmd.ExecuteReader()
                         While reader.Read
                              Deplist = New ListItem()
                              Deplist.Value = reader("Department")
                              Deplist.Text = reader("Department")
                              DDLDepartment.Items.Add(Deplist)
                         End While
                    End Using       
               End Using    
          End Using
     End Sub
    

    I don’t see any reason for you to try to return all three results in a single query. That will just make your code unnecessarily complicated just to save a millisecond or two. Connection pooling handles the creation of connections on the database server for you, so opening a new connection in your code is very fast.

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

Sidebar

Related Questions

I am currently working on a mobile web page in asp.Net MVC2 that allows
I am currently working on an web application that uses ASP.NET 2.0 framework. I
I'm currently working on an ASP.NET web-site. In one of my pages, I had
I'm currently working on an Asp.net MVC Web application. In my solution, there're two
The ASP.NET web app project I'm currently working on is getting slower over time
I'm currently working on a reasonably complicated data input form, based around ASP.NET Web
I'm currently working on web application using VB in ASP.NET. Right now I have
I am working on an ASP.NET 2.0 web solution which currently runs en-GB and
I am working on an asp.net/C# web application, In the main page: I have
I am currently working with a ASP.NET Web site project in Visual Studio 2008

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.