DropDownList
-
Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel
-
Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel
did you search before you post ?
I Wish the Life Had CTRL-Z Wizard's First Rule : People are fool,they believe what they want to believe or what they afraid to believe www.subaitech.blogspot.com
-
Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel
Hi, Im not sure for how depth u except the answer, nor whether you have a layered architecture in place. There are couples of ways of achiving this… Let’s assume that I have database named TestDB and it has a Employee table which has, EmployeeNumber, Name and some other fields. My objective is to get the EmployeeIDs and Names and bind them to the control. The stored procedure ill be using is just a simple one as Select EmployeeNumber, Name from Employee and I will call this sp as Employee_Select. Here is a quick way… • Let’s configure a DataSource to your DropDownList. When configuring a datasource, you need to select the database and connect to it and select the stored procedure you want to use to populate the data to the control. So here is the sample code for that. <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsTestSource" DataTextField="Name" DataValueField="EmployeeNumber"> </asp:DropDownList><asp:SqlDataSource ID="dsTestSource" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="Employee_Select" SelectCommandType="StoredProcedure"></asp:SqlDataSource> Now let’s do it in a structured way… 1. Get the data from the Database. a. Create a Sqlconnection object, and create a Sqlcommand and execute it agaist your database and use an adapter to fill the data to a DataTable. Ex: string sConnection = ConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString; SqlConnection Con = new SqlConnection(sConnection); SqlCommand cmd = new SqlCommand(); cmd.Connection = Con; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Employee_Select"; SqlDataAdapter adp = new SqlDataAdapter(cmd); DataTable dtValues = new DataTable(); adp.Fill(dtValues); 2. Bind it to the control. DropDownList1.DataSource = dtValues; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeNumber"; DropDownList1.DataBind(); You may write the above code in a method and call it when you need to fill the control. Hope its clear to you.. Thx, Gayani
modified on Saturday, July 5, 2008 4:26 AM
-
Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel
Execute the stored procedure sp_databases in master database. Then bind the returned result( say its a datatable of name dt) with the dropdown list like this: .DataTextField="Database_Name"; .DataValueField="Database_Name"; .DataSource=dt; .DataBind(); Regards, Wasif.