1,105,334 Community Members

get data from database in dropdown list

Member Avatar
anjela
Newbie Poster
3 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i'm developing application in .net using c# that has 1 dropdown list .I want to take data in it .to show in list.please suggest me some help

Member Avatar
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

select the info that you want into a datatable,
set the datatable as the datasource for the dropdown. call the databind function. and make sure you specify in the dropdown control which columns have correspond with the DataValueField and DataTextField

Member Avatar
anjela
Newbie Poster
3 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

can u give code for the get data from database in dropdown list

Member Avatar
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 2 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 
<asp:SqlDataSource ID="sda_nw1" runat="server" ConnectionString='<%$ ConnectionStrings:NORTHWIND %> ' 
        SelectCommand="SELECT orders.customerid, ContactName, count(OrderID) as numorders FROM orders join customers on orders.CustomerID=Customers.CustomerID group by orders.customerid, ContactName order by ContactName" 
        ProviderName="System.Data.SqlClient" OldValuesParameterFormatString="original_{0}">
        </asp:SqlDataSource>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sda_nw1" DataTextField="customerid"
            DataValueField="customerid">
        </asp:DropDownList><br />
Member Avatar
sachingargava
Newbie Poster
1 post since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

please give me the source code to retieve data from database todropdownlist in c#.net

Member Avatar
sanch01r
Light Poster
30 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

You can add data that is returned from a reader the same way you would programmatically add items to a combobox (combobox1.Items.Add(*Object to add to combobox*);

Here is a sample of code that I did a while ago, this reads all of the Columns on a particular table and adds it to a combobox.

for (int i = 0; i < dr.FieldCount; i++)
            {
                Fields.Add(dr.GetName(i));
                comboBox1.Items.Add(dr.GetName(i));


            }
Member Avatar
asshankar
Newbie Poster
1 post since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i'm developing application in .net using c# that has 1 dropdown list .I want to take data in it .to show in list.please suggest me some help

eg: Label=DropDownlist

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

    }
    public void loadFrom()
    {
        string insertstring = @"select * from ddlFFromDB";
        SqlConnection conn = new SqlConnection("Server=SSK-          PC\\MSSQL2008;database=*******;User ID=******;Password=*********;");
        conn.Open();
        SqlCommand cmd = new SqlCommand(insertstring, conn);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet data = new DataSet();
        adapter.Fill(data);
        conn.Close();


        ddlFrom.DataSource=data.Tables[0];
        ddlFrom.DataValueField = "FromId";
        ddlFrom.DataTextField = "From";
        ddlFrom.DataBind();

    }
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article