Gold
Gold

Reputation: 62424

how to fill DropDownList from database in asp.net?

how to fill DropDownList from database in asp.net ?

and when i pick value from the DropDownList how to catch this event ?

Conn.Open();
SQL = "SELECT distinct city FROM MEN";
dsView = new DataSet();
adp = new SqlDataAdapter(SQL, Conn);
adp.Fill(dsView, "MEN");
adp.Dispose();

DropDownList1. ?????? (what do to ?)

thanks in advance

Upvotes: 3

Views: 38529

Answers (6)

Nimitha madhavram
Nimitha madhavram

Reputation: 31

First take the details in a dataset then the following code will help you:

DropDownList1.DataSource = ds
DropDownList1.DataTextField = "emailid"
DropDownList1.DataValueField = "userid"
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, New ListItem("select", "-1"))

Upvotes: 3

MohammedAshrafali
MohammedAshrafali

Reputation: 569

Another way to bind dropdownlist is...

<asp:DropDownList ID="ddlCity" runat="server" DataValueField="pkId" DataTextField="cityName" DataSourceID="sqlDB">
    </asp:DropDownList>
    <asp:SqlDataSource ID="sqlDB" ConnectionString='$Name of connecitonstring' runat="server" SelectCommand="Select * from tbl_City"></asp:SqlDataSource>

Upvotes: 0

Alex Mathew
Alex Mathew

Reputation: 350

//...Wrote separate class for calling this function
FunctionClass obj = new FunctionClass();
List<Designation> details = new List<Designation>();
bool result1 = obj.DataDrop(out details);
if (result1 == true)
{
dropDownDesignation.DataSource = details;
dropDownDesignation.DataTextField = "designation";
dropDownDesignation.DataValueField = "Designation_ID";
dropDownDesignation.DataBind();
dropDownDesignation.Items.Insert(0, new ListItem("--Select--", "0"));
}

//..This function wrote inside FunctionClass and called from aspx.cs page
public bool DataDrop(out List<Designation> designationDetails)
{
designationDetails = new List<Designation>();
conn = new SqlConnection(connectionName);
conn.Open();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "DesignationDetails";
userReader = command.ExecuteReader();
if(userReader.HasRows)
{
    while(userReader.Read())
{
    designationDetails.Add(new Designation()
    {
            designationId=userReader.GetInt32(0),
            designation=userReader.GetString(1)
        });
 }
}
return true;
}
//..This should declare outside the class but inside the namespace
public class Designation
{
public int designationId { get; set; }
public string designation { get; set; }
}

Upvotes: 0

Kasrak
Kasrak

Reputation: 1561

This could be a complete walkthrough for you in this case :

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

protected void Page_Init(object sender, EventArgs e)
{ 

        SqlDataSource sqlDS = new SqlDataSource();
        sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings[0].ToString();
        sqlDS.SelectCommand = "select GenderID,Gender from mylookupGender";
        form1.Controls.Add(sqlDS);

        DropDownList ddl = new DropDownList();
        ddl.ID = "dddlGender";
        ddl.DataSource = sqlDS;
        ddl.DataTextField = "Gender";
        ddl.DataValueField = "GenderID";
        form1.Controls.Add(ddl);

        // ... Repeat above code 9 times or put in a for loop if they're all the same...
}

private void BindDropDownLists()
{
    foreach (Control ctl in form1.Controls)
    {
        if (ctl is DropDownList)
        {
            (ctl as DropDownList).DataBind();
        }
    }
}

Upvotes: 0

jjj
jjj

Reputation: 605

Simple sample code :

DropDownList.DataSource = yourDataSource;
DropDownList.DataTextField = "displayNameColumnName ";
DropDownList.DataValueField = "TheValueColumnName";
DropDownList.DataBind();

Upvotes: 1

Oded
Oded

Reputation: 498914

You set the DataSource, DataTextField and DataValueField and call DataBind() in order to populate the dropdownlist.

The datasource can be pretty much any IEnumerable and the text and value will be looked up with reflection.

The event you want to catch is the SelectedIndexChanged event - this will fire when you change the selection.

Upvotes: 5

Related Questions