?
Solved

how do i bind dropdownlist to database asp .net C#

Posted on 2009-04-27
2
Medium Priority
?
302 Views
Last Modified: 2013-11-08
Expert: I have a .net asp app with an INSERT and EDIT mode and a dropdownlist with 3  items "Choose type of expert" , "expert"  and "superexpert"  to choose form repectively.  I want to bind the dropdownlist with a back ended database such that when user chooses one of these items I want to set a datafield called "ExpertType" with the selected item. I am not sure how to bind the dropdownlist for insert and edit mode. Could you please fix my rough code below so it binds and works correctly. When web page first appears dropdownlist default is "Choose type of expert" . thanks for any help here.
<asp:DropDownList ID="ExpertDdl" runat="server" 
SelectedValue='<%# Bind("ExpertType") %>'Width="92px">
<asp:ListItem Selected=True Value="-1">-- Choose type of expert--</asp:ListItem>
<asp:ListItem Text="expert" Value="expert"></asp:ListItem>
 <asp:ListItem Text="superexpert" Value="superexpert"></asp:ListItem></asp:DropDownList>

Open in new window

0
Comment
Question by:tcmmaxt
2 Comments
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 1200 total points
ID: 24245313
try this
<asp:DropDownList ID="ExpertDdl" runat="server" 
SelectedValue='<%# Bind("ExpertType") %>'Width="92px">
<asp:ListItem Value="-1">-- Choose type of expert--</asp:ListItem>
<asp:ListItem Text="expert" Value="expert"></asp:ListItem>
 <asp:ListItem Text="superexpert" Value="superexpert"></asp:ListItem></asp:DropDownList>

Open in new window

0
 
LVL 10

Assisted Solution

by:Christian de Bellefeuille
Christian de Bellefeuille earned 800 total points
ID: 24245359
It look just fine for me.  What's wrong with it?  ExpertType must be a string type (varchar or anything like a string).

Of course you have to define it in your SQLDataSource insert or update... (or you have to handle it in your code behind).

Something like this:
<UpdateParameters>
     <asp:ControlParameter name="ddlChoice" DbType="String" ControlID="ExpertDdl" PropertyName="SelectedValue" />
</UpdateParameters>

and don't forget to put this parameter in your update query like this:
"... SET ExpertType = @ddlchoice WHERE ..."



0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 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