Solved

dynamic ddl

Posted on 2009-05-15
7
301 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
ID: 24399620
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
ID: 24399669
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
ID: 24399755
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Romacali
ID: 24399916
I will try it out
0
 

Author Comment

by:Romacali
ID: 24400086
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
ID: 24402912
I would have to see your code for saving.
0
 
LVL 1

Expert Comment

by:marktoth
ID: 24408317
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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