1.11M Members

get data from database in dropdown list

 
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

 
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

 
0
 

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

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

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

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


            }
 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article