Solved

dynamic ddl

Posted on 2009-05-15
7
304 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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