Click here to Skip to main content
15,884,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi experts..............

how to fill the data from table to dropdownlist in asp.net c#

I have a table in Sqldatabase as colleges, this colleges list must be displayed in dropdownlist using code.

This is my CollegeTable

CollegeCode CollegeName
HITS Hipoint engg college
WITS Wisdom engg college
SITS Shaaz engg college
PITS Padmavathi engg college


Now i want to display collegeCodes in dropdownlist using asp.net c# code.

Please help

Thanks in @dvance.
Posted

1) Get data from your database and fill it to datatable like dtCollege
C#
SqlConnection con = new SqlConnection("");//connection name
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dtCollege = new DataTable();
da.Fill(dtCollege);

2) set two properties of your dropdown list
C#
//If you need to display college name to dropdown list
   ddl.DataValueField= "CollegeCode";
   ddl.DataTextField= "CollegeName";

//OR

//If you need to display college Code to dropdown list
   ddl.DataValueField= "CollegeCode";
   ddl.DataTextField= "CollegeCode";

3) then assign data-source to your dropdown
C#
ddl.DataSource = dtCollege;
  ddl.DataBind();


4) Finally code looks like this with "----Select College-----"
C#
SqlConnection con = new SqlConnection("");//connection name
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dtCollege = new DataTable();
da.Fill(dtCollege);
//If you need to display college name to dropdown list
ddl.DataValueField= "CollegeCode";
ddl.DataTextField= "CollegeName";

//OR

//If you need to display college Code to dropdown list
ddl.DataValueField= "CollegeCode";
ddl.DataTextField= "CollegeCode";

ddl.DataSource = dtCollege;
ddl.DataBind();
ddl.Items.Insert(0,"---Select College----");





that's it, your dropdownlist is bind with your database table data.
 
Share this answer
 
v6
Comments
Ranjith Reddy CSE 17-Apr-13 5:16am    
please help, am fresher to asp.net
Tejas Vaishnav 17-Apr-13 5:26am    
check it now, if you found this is your solution then please accept it as answer and also do not forget to rate it, so it will help others in future for same problem.
Ranjith Reddy CSE 17-Apr-13 5:31am    
Its working fine, but i need like this
----- Select College -----

how should we write this in code.
N ManojKumar 17-Apr-13 5:41am    
ddl.DataSource = dtCollege;
ddl.Items.Insert(0, "----- Select College -----");
ddl.DataBind();
Ranjith Reddy CSE 17-Apr-13 5:54am    
This is my code, it doesnot display SELECT option.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_Departments();
}

private void Fill_College()
{
SqlConnection con = new SqlConnection(_connString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DDLCollege.DataSource = dt;
DDLCollege.Items.Insert(0, "------ Select College -----");
DDLCollege.DataTextField = "CollegeCode";
DDLCollege.DataValueField = "CollegeName";
DDLCollege.DataBind();
}

Please help,

I want to desplay --- Select college ----

but it doesnot display, please chk my code.
Hi,

1st get the data from the database by a stored procedure or simply a sql command. Then keep the data into a data table and feed the datatable data to the dropdownlist.

Code snipet here:

SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=test;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("select * from hospital", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DropdownList.DataSource = dt;
            DropdownList.DataTextField = "CURRENCY_SHORT_DESCRIPTION";
            DropdownList.DataValueField = "CURRENCY_SHORT_DESCRIPTION";
            DropdownList.DataBind();


Thanks
 
Share this answer
 
Comments
Ranjith Reddy CSE 17-Apr-13 6:07am    
This is my code, it doesnot display SELECT option.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_Departments();
}

private void Fill_Departments()
{
SqlConnection con = new SqlConnection(_connString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DDLCollege.DataSource = dt;
DDLCollege.Items.Insert(0, "------ Select College -----");
DDLCollege.DataTextField = "CollegeCode";
DDLCollege.DataValueField = "CollegeName";
DDLCollege.DataBind();
}

Please help,

I want to desplay --- Select college ----

but it doesnot display, please chk my code.
codeninja-C# 17-Apr-13 7:54am    
Hi,
try to add insert items into ddl after data binding data into ddl
DDLCollege.DataBind();
DDLCollege.Items.Insert(0, "------ Select College -----");
--Sj
[no name] 17-Apr-13 10:15am    
Change your code to as follows:
SqlConnection con = new SqlConnection(_connString); con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(); da.Fill(dt);
DDLCollege.DataSource = dt;
DDLCollege.DataTextField = "CollegeCode";
DDLCollege.DataValueField = "CollegeName";
DDLCollege.DataBind();
DDLCollege.Items.Insert(0, "------ Select College -----");
Hi,

try this code..

C#
SqlConnection con = new SqlConnection("connection string");// create connection string
con.Open();   // open the connection
SqlCommand cmd=new SqlCommand("Select CollegeCode, CollegeName from CollegeTable",con);
              // call the data from database 
SqlDataAdapter da=new SqlDataAdapter(cmd); // communicate between Dataset and DB
DataSet ds=new DataSet();
da.Fill(ds); 

//Bind the result set to DropDownList 
dropdownList1. DataSource=ds.Tables[0];  
dropdownList1.DisplayMember ="CollegeName"; 
dropdownList1.ValueMember ="CollegeCode"; 
dropdownList1.DataBind();

ListItem lstSelect = new ListItem();
lstSelect.Text = "----Select----->";
lstSelect.Value = "";
dropdownList1.Items.Insert(0, lstSelect);


if you want more information regarding this refer to the below link...

http://www.codersource.net/AspNet/AspNetArticles/DropDownListinASPNet.aspx[^]
 
Share this answer
 
v2
Comments
Ranjith Reddy CSE 17-Apr-13 6:07am    
This is my code, it doesnot display SELECT option.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_Departments();
}

private void Fill_Departments()
{
SqlConnection con = new SqlConnection(_connString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DDLCollege.DataSource = dt;
DDLCollege.Items.Insert(0, "------ Select College -----");
DDLCollege.DataTextField = "CollegeCode";
DDLCollege.DataValueField = "CollegeName";
DDLCollege.DataBind();
}

Please help,

I want to desplay --- Select college ----

but it doesnot display, please chk my code.
Try This.........

Design the DropDownList like this..
XML
<asp:DropDownList ID="ddl" runat="server">
   <asp:ListItem >Select </asp:ListItem>


Code Like this..

SqlConnection con = new SqlConnection(YourDB_connString_Here);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddl.ClearSelection();
ddl.DataSource = dt;
ddl.DataTextField = "EmpCode";
ddl.DataValueField = "EmpName";
ddl.DataBind();


First Clear Selection then Bind Data to ddl then automatically it shows select in ddl
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900