Solved

dynamic ddl

Posted on 2009-05-15
7
300 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

23 Experts available now in Live!

Get 1:1 Help Now