We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,078 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

get data from database in dropdown list

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

6
Contributors
6
Replies
4 Years
Discussion Span
2 Years Ago
Last Updated
9
Views
anjela
Newbie Poster
3 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
Skill Endorsements: 0

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

anjela
Newbie Poster
3 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 />
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0

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

sachingargava
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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));


            }
sanch01r
Light Poster
30 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 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();

    }
asshankar
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0871 seconds using 2.66MB