Click here to Skip to main content
Licence 
First Posted 23 Mar 2004
Views 71,347
Bookmarked 20 times

Database connected DropDownList

By | 23 Mar 2004 | Article
This control is connected to a database table to populate Items value in DropDownList control.

Introduction

This is a data-bound dropdown list control (VB.NET) that is inherited from System.Web.UI.WebControls.DropDownList server control.

Some times you have the requirement to populate the DropDownList from any database table’s column with some filters. One way to accomplish this would be making a DataSet and fill it with result of the query or stored procedure and then source the DropDownList from that DataSet. The other approach would be to have a UserControl which has properties for connection string, table name, columns to display, column for value. The control discussed here in this article builds the query according to the values of these properties and pulls the data from database.

Properties

This control has the following properties:

  • TableName: from which you want to populate the data.
  • Connection_String: This is a place where you can put the connection string. It’s not really a good place to put the connection string, so if you want, you can also specify that in web.config and get the application variable from there. The control will pick this field first, and if this is Nothing, then it will look for any application variable called “connectionString”. You are welcome to modify the code according to your need.
  • DisplayColumn: Exact name of the column from the database table which you want to display in the DropDownList.
  • ValueColumn: Column name from the database which you want as value field in DropDownList.
  • FirstField: True or False will decide whether to put any first value.
  • FirstFieldText: This value will go into the first field of DropDownList. This is used if you just want to display something <-Please choose bla bla bla -->.
  • DisplayColumnSeparator: If you want to display two column fields in the DropDownList with some separator, then this one comes in the picture. Example: let’s say database columns are FirstName and LastName, but you want to display FirstName#LastName in the DropDownList, then you will put “#” in the DisplayColumnSeparator property.
  • ValueColumnSeparator: This also works the exactly the same way as DisplayColumnSeparator other than this value goes into the “Value” field of DropDownList.
  • WhereClause: This text will goes into the SQL query’s where clause. This property is optional and very useful in filtering the data from the table. For example: “ Where UserID =1”.

HTML to add inline

<%@ Register TagPrefix="ddldb" 
  namespace= WebApplication1 assembly=WebApplication1 %>
<ddldb:dbdropdownlist id="ddldb1" runat="server" width="216px" >
</ddldb:dbdropdownlist>

Issues with this control

  • Uses query to access data. Not written to use sproc, but can be modified according to the need.
  • Many more yet to appear :).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Neeraj Jain

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVisual Web Developer 2005 Express Edition PinmemberZvedavec12:27 13 Nov '07  
QuestionHow to use this? Pinmemberdraynumfa6:49 29 Jan '05  
GeneralUsing The Control PinmemberRamziAbbb9:35 9 Nov '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 24 Mar 2004
Article Copyright 2004 by Neeraj Jain
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid