Solved

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

Posted on 2009-05-07
3
272 Views
Last Modified: 2013-11-08
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
Comment
Question by:tcmmaxt
  • 2
3 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
Comment Utility
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
 

Author Comment

by:tcmmaxt
Comment Utility
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
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

11 Experts available now in Live!

Get 1:1 Help Now