Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server , +
Hi Experts !!
 
I have a dropdownlist in my aspx page.
 
I want to take the data from database and place in this dropdownlist. whatever the data is in table it must display in dropdown.
 
and when ever i add new item in table in should display in dropdownlist.
 
Please can you help me, how to do this.
 
Thanks.
Posted 23-Feb-13 20:07pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

protected void Page_Load(object sender, EventArgs e)
        {
If(!IsPostBack)
  BindDropDownList();
}
 
private void BindDropDownList()
{
   DataTable dtData = new DataTable();
   dtData = //GetDataFromDb();
   this.Ddl.DataSource = dtData;
   this.Ddl.DataValueField = "ID";
   this.Ddl.DataTextField = "Name";
   this.Ddl.DataBind();
}
 
protected void BtnAdd_Click(object sender, EventArgs e)
{
    // Add data in DB
   BindDropDownList();
}
 
Please follow this link
 
http://stackoverflow.com/questions/4242766/binding-a-dropdownlist-to-a-database[^][]
  Permalink  
v3
Comments
Ranjith Reddy CSE at 24-Feb-13 2:21am
   
No need of Sql Connections here.....Why ?
Asim Mahmood at 24-Feb-13 2:24am
   
this method is showing GetDataFromDb(); you'll get data from you DataBase like BLL.GetDataFromDb();. if you dont have idea how to build SQLConnection it's another point.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Several ways to do this:
 
you can write sql command that pull the data from the database as
 
SqlCommand cmd = new SqlCommand("Select Value1,Value2 from table");
 
And then specify the datasource of your dropdownlist as follows:
dropdownlist1.datasource() cmd.executereader();
 
dropdownlist1.datatextfield = "Value1" -- this means Value1 is what you want to show in the drop down
 
dropdownlist1.databind(); -- this what will do the final binding when you run the page.
 
There more resources and difference ways. Feel free to read more about different ways of doing things.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dropdownlist_Insertshow._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Name" Width="100px"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="btn_add" runat="server" Text="Add Name" onclick="btn_add_Click"
            Width="100px" /><br />
    <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList><br />
 
    </div>
    </form>
</body>
</html>
 

 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_Insertshow
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
 
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropbind();
}
}
 
protected void btn_add_Click(object sender, EventArgs e)
{
DropDownList1.Items.Clear();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Insert into employee values('" + TextBox1.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
Response.Write("Records successfully inserted");
clear();
dropbind();
}
private void clear()
{
TextBox1.Text = "";
}
private void dropbind()
{

SqlConnection con = new SqlConnection(strConnString);
DropDownList1.Items.Add("Choose Name");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["name"].ToString());
}
reader.Close();
con.Close();
}

}
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 489
1 ChintanShukla 295
2 Gihan Liyanage 250
3 Richard Deeming 250
4 RyanDev 250
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,601
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,923


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100