Solved

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

Posted on 2009-05-07
3
273 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
ID: 24329154
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
ID: 24329243
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
ID: 24329305
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

15 Experts available now in Live!

Get 1:1 Help Now