943,771 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 42832
  • C# RSS
Feb 21st, 2007
0

get data from database in dropdown list

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anjela is offline Offline
3 posts
since Feb 2007
Feb 21st, 2007
0

Re: get data from database in dropdown list

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
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Feb 22nd, 2007
0

Re: get data from database in dropdown list

can u give code for the get data from database in dropdown list
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anjela is offline Offline
3 posts
since Feb 2007
Feb 22nd, 2007
0

Re: get data from database in dropdown list

C# Syntax (Toggle Plain Text)
  1. <asp:SqlDataSource ID="sda_nw1" runat="server" ConnectionString='<%$ ConnectionStrings:NORTHWIND %> '
  2. 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"
  3. ProviderName="System.Data.SqlClient" OldValuesParameterFormatString="original_{0}">
  4. </asp:SqlDataSource>
  5.  
  6. <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sda_nw1" DataTextField="customerid"
  7. DataValueField="customerid">
  8. </asp:DropDownList><br />
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Nov 10th, 2009
-1
Re: get data from database in dropdown list
please give me the source code to retieve data from database todropdownlist in c#.net
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sachingargava is offline Offline
1 posts
since Nov 2009
Nov 15th, 2009
0
Re: get data from database in dropdown list
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.

C# Syntax (Toggle Plain Text)
  1. for (int i = 0; i < dr.FieldCount; i++)
  2. {
  3. Fields.Add(dr.GetName(i));
  4. comboBox1.Items.Add(dr.GetName(i));
  5.  
  6.  
  7. }
Last edited by sanch01r; Nov 15th, 2009 at 4:48 pm.
Reputation Points: 10
Solved Threads: 3
Light Poster
sanch01r is offline Offline
30 posts
since Oct 2009
Apr 30th, 2011
0
Re: get data from database in dropdown list
Click to Expand / Collapse  Quote originally posted by anjela ...
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();

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asshankar is offline Offline
1 posts
since Apr 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Saving Options: File or DataBase
Next Thread in C# Forum Timeline: Login form using C# and SQL Server 2005





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC