Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Database connected DropDownList

0.00/5 (No votes)
23 Mar 2004 1  
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