how to work on dropdown list in 3.5

Posted by Shwetabansal888 under Regular Expressions on 6/28/2010 | Views : 1798 | Status : [Member] | Replies : 1
Plz tell me how would I bind data from database using with dropdownlist using 3.5 and also On selection of item from ddl a new dropdownlist will be created.Plz let me send the code its very urgent.


Posted by: Vuyiswamb on: 6/28/2010 [Member] [MVP] [Administrator] NotApplicable

There are many Options, First you can bind the Dropdown with a Datatable or dataset or a Reader like this

I create a Small Project for you that i have attached. Let me explain the code, Create a Function that will Connect to the DB and Bind the dropdownlist like this

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

if (!Page.IsPostBack)


private void BindDropdown()
String COnstring = @"user id=sa;Password=oops;Server=VUYISWA\SSIS_Server;Database=VUYISWA";

SqlConnection Con = new SqlConnection(COnstring);

SqlCommand cmdselect = new SqlCommand();

cmdselect.CommandText = "select ID from dbo.TestTable";

cmdselect.CommandType = CommandType.Text;

cmdselect.Connection = Con;

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmdselect;

DataSet ds = new DataSet();



if(ds.Tables[0].Rows.Count > 0)

DropDownList1.DataSource = ds;
DropDownList1.DataTextField = ds.Tables[0].Columns[0].ToString();
DropDownList1.DataValueField = ds.Tables[0].Columns[0].ToString();
catch(SqlException ex)
lblselected.Text = ex.Message;

and when the user selected the dropdownlist the selectedindexchanged event will be fired and the following code will assign a label to the selected item like this

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

String test = DropDownList1.SelectedItem.Text;

lblselected.Text = test;


Thank you for posting at Dotnetfunda

Vuyiswa Maseko

 Download source file

Thank you for posting at Dotnetfunda

Shwetabansal888, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response