Solved

dynamic ddl

Posted on 2009-05-15
7
299 Views
Last Modified: 2015-07-29
HI,

I have a ddl in my page that should get the values from the database table type?
I'm new to ASP.net
how can I add it to my page?

so far I hardcode as:
<asp:DropDownList ID="ddlEvaluationType" runat="server" CssClass="ddl">

                    <asp:ListItem Value="1" >Program Begin</asp:ListItem>

                    <asp:ListItem Value="2" >Program End</asp:ListItem>

                    <asp:ListItem Value="3" >6 Months</asp:ListItem>

                    </asp:DropDownList>

Open in new window

0
Comment
Question by:Romacali
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:marktoth
Comment Utility
There are a few ways to do this.  
You can use an sqldatasource control placed directly on the aspx page
OR
you can use the code behind file to retrieve data from the database, set the ddl's datasource property and call databind() on the dropdown list

Which way do you want to know?
0
 

Author Comment

by:Romacali
Comment Utility
I think the  sqldatasource control placed directly on the aspx page is easier :)

thanks
0
 
LVL 1

Accepted Solution

by:
marktoth earned 500 total points
Comment Utility
To use an SqlDataSource take the following steps
1.  On the .aspx page where your dropdownlist is place a SqlDataSource Control
2.  In design view on the page select the DataSourceControl you placed on the page (you'll see a small arrow in the upper right corner when you select it) click on the arrow and from the popup menu click the Configure DataSource item. This will bring up the Configure DataSource Dialog box.
3.  Select your connection string or create a New Connection string and click Next.
4.  in the next screen choose the Specify columns from a table or view radio button
5.  in the name dropdown list select the table you want to select data from.
6.  in the coluns list box select the two columns you want to retrieve data from.
7.  Click the next button.
8.  You can test the query in this new screen by clicking the test query button OR click the finish button to complete you're datasource setup.
9.  Now back in design view of your .aspx page right click on your dropdownlist control and select properties from the menu.
10.  In the properties window select the DataSourceId propery (a down arrow will appear next to the property value), click the arrow and select the SqlDataSource Control you just added.
11. In the properties window select the DataTextField property and select the column from the database table that you want displayed as text in the dropdownlist
12.  In the properties window select the DataValueField property and select the column frm the database table that you want to use as the value field.
13.  Now run the page and your dropdownlist should fill with test and values from the databasae.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Romacali
Comment Utility
I will try it out
0
 

Author Comment

by:Romacali
Comment Utility
ok..

now the value is not saving.. Now can I save it?

I had this code:
     cmd3.Parameters.Add(new SqlParameter("@EvaluationID", ddlEvaluationType.SelectedValue));
0
 
LVL 1

Expert Comment

by:marktoth
Comment Utility
I would have to see your code for saving.
0
 
LVL 1

Expert Comment

by:marktoth
Comment Utility
Actually looking at it.  You probably need to convert the selected value to an integer type for your save.
It's important that you match your database datatype to the type you're passing to your stored proc.
More than likely your storing the EvaluationTypeId as an integer in the database. ddlEvaluationType.SelectedValue returns a type string so convert.
cmd3.Parameters.Add(new SqlParameter("@EvaluationID", Convert.ToInt32(ddlEvaluationType.SelectedValue));
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now