954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

anjela
Newbie Poster
3 posts since Feb 2007
Reputation Points: 10
Solved Threads: 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
 

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
 
<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>
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 13
Solved Threads: 4
 

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
 

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
 
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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You