• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

how do you bind dropdownlist with in edit mode with item is not in the list

expert: I have a asp .net C# web INSERT and EDIT Mode FormView with a dropdownlist with "make a selection" as one of the choices. The DDL is loaded from a db table list which does not include "make a selection". When user does not make a selection a NULL is inserted into the db record . However when I bring the record up and click the edit link I get error "SelectedValue which is invalid because it does not exist in the list of items". I am not sure how to fix this issue?  My code is below:
<asp:DropDownList ID="MYDdl" runat="server" AppendDataBoundItems="True"
                                        DataSourceID="MYTypeDataSource" DataTextField="car_type"    
                                        DataValueField="car_type"
                                        SelectedValue='<%# Bind("car_type") %>' Width="200px">
                                        <asp:ListItem Value="-1">--make a selection --</asp:ListItem>
                      </asp:DropDownList>
 
                  // C# code behind
                   if (MYDdl.SelectedValue == "-1")
                    {
                        e.Values["car_type"] = null;
                      
                    }

Open in new window

0
tcmmaxt
Asked:
tcmmaxt
  • 2
1 Solution
 
mohan_sekarCommented:
What do you want to do -> handle the case where you get a null value from the database? Or, prevent inserting a null value in to the database?
0
 
tcmmaxtAuthor Commented:
expert: What do you want to do -> handle the case where you get a null value from the database
Yes -->handle the case where you get a null value from the database
0
 
mohan_sekarCommented:
Ok, you got to do two things.
Set 'AppendDataBoundItems' to True for the Dropdown list. In Page_Load, add the following line
dropdownlist.items.add("Please make a selection");

Step 2: when you get a null value from the database, select the 1st item
dropdownlist.selectedindex = 0;
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now