Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / Visual Basic
Article

Database connected DropDownList

Rate me:
Please Sign up or sign in to vote.
3.25/5 (8 votes)
23 Mar 20042 min read 82.9K   259   20   2
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

ASP.NET
<%@ 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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to use this? Pin
draynumfa29-Jan-05 6:49
draynumfa29-Jan-05 6:49 
GeneralUsing The Control Pin
RamziAbbb9-Nov-04 9:35
RamziAbbb9-Nov-04 9:35 
What steps do I have to take to actually use this control on an aspx page?

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

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