Solved

dynamic ddl

Posted on 2009-05-15
7
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

726 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