Solved

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

Posted on 2009-05-07
3
274 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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