473,840 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList has SelectedValue which in invalid

I have a DropDownList in an ASP.NET web form that is populated with items
from a lookup table by binding that DropDownList to a SqlDataSource.
However, the items in the lookup table can change over time. The problem is
that when an item has been removed from the lookup table, and a user wants
to retrieve a record that used the deleted item, the following error occurs:

'ddlAssignedTo' has a SelectedValue which is invalid because it does not
exist in the list of items.

I need to allow the lookup table items to change, while still allowing
retrieval of old records that used the now deleted items. I could add a
field to the lookup table indicating whether an item is active or inactive,
but only the active ones (and the selected one) should appear in the
DropDownList.

So, is there a way to either

a) Allow entries into a DropDownList that are not in the databound table
(like a VB ComboBox), or

b) Add the missing value to the DropDownList items after it is bound to
the table?

Thanks,

Brett
Sep 19 '08 #1
1 4954
Hi Brett,

From your description you want to avoid getting the exception:
'ddlAssignedTo' has a SelectedValue which is invalid because it does not
exist in the list of items.

Is my understanding correct?

If so you don't have to add an extra field in the data base. This exception
is generally caused by binding the DropDownList on every postback. To
demonstrate how this exception will be thrown I've created a demo:

aspx.cs
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
//Uncomment following code to reproduce this exception
// this.DropDownLi st1.DataBind();
}

protected void Button1_Click(o bject sender, EventArgs e)
{
this.DropDownLi st1.SelectedVal ue = this.TextBox1.T ext;
}
}
aspx:
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:AConnectionS tring %>"
SelectCommand=" SELECT * FROM [Table_1]"></asp:SqlDataSour ce>

<asp:DropDownLi st ID="DropDownLis t1" runat="server"
DataSourceID="S qlDataSource1" DataTextField=" theID"
DataValueField= "theID">
</asp:DropDownLis t>

<asp:Button ID="Button1" runat="server" onclick="Button 1_Click"
style="height: 26px" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>

To reproduce please try following steps:
1. View the page and enter an item of the DropDownList in the TextBox.
2. Delete the record in the data base where the above item resides.
3. Click the button to post back.

Initially there's no exception. If you uncomment the code in the Page_Load
event handler the exception will be thrown.

So if we want to eliminate this exception, we should check if we bind the
DropDownList on every postback. If it's done in code behind, following code
may always help:

if(!IsPostBack)
{
//Bind DropDownList
}

If binding the DropDownList on every postback is a must in the requirement
I think we can do some check before setting the selected value of the
DropDownList.

As to your following concerns:
=============== =============== =============== =====
So, is there a way to either

a) Allow entries into a DropDownList that are not in the databound table
(like a VB ComboBox), or

b) Add the missing value to the DropDownList items after it is bound to
the table?
=============== =============== =============== =====

We can add an item in the PreRender event handler of the DropDownList
control. Something like:
void DropDownList1_P reRender(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)s ender;
ddl.Items.Inser t(0, new ListItem("hello ", "world"));
}

If this problem isn't got solved please send me a demo project that can
help me understand your scenario better.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Brett" <br*****@newsgr oup.nospam>
| Subject: DropDownList has SelectedValue which in invalid
| Date: Fri, 19 Sep 2008 17:42:02 -0400
| Lines: 38
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
| X-RFC2646: Format=Flowed; Original
| Message-ID: <up************ **@TK2MSFTNGP02 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: dsl-1-210.d01.scpnbh. pbtcomm.net 64.53.27.210
| Path: TK2MSFTNGHUB02. phx.gbl!TK2MSFT NGP01.phx.gbl!T K2MSFTNGP02.phx .gbl
| Xref: TK2MSFTNGHUB02. phx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:7641 7
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
|
| I have a DropDownList in an ASP.NET web form that is populated with items
| from a lookup table by binding that DropDownList to a SqlDataSource.
| However, the items in the lookup table can change over time. The problem
is
| that when an item has been removed from the lookup table, and a user
wants
| to retrieve a record that used the deleted item, the following error
occurs:
|
|
|
| 'ddlAssignedTo' has a SelectedValue which is invalid because it does not
| exist in the list of items.
|
|
|
| I need to allow the lookup table items to change, while still allowing
| retrieval of old records that used the now deleted items. I could add a
| field to the lookup table indicating whether an item is active or
inactive,
| but only the active ones (and the selected one) should appear in the
| DropDownList.
|
|
|
| So, is there a way to either
|
| a) Allow entries into a DropDownList that are not in the databound
table
| (like a VB ComboBox), or
|
| b) Add the missing value to the DropDownList items after it is bound
to
| the table?
|
|
|
|
|
| Thanks,
|
| Brett
|
|
|

Sep 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3128
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit is performed. How do I force the usercontrol to display the newly assigned date? I don't have this problem if I place the two dropdownlist and the year field directly on the aspx page.
15
26938
by: Swetha | last post by:
Hello I have a DropDownList that I am populating using the following SqlDataSource: <asp:DropDownList ID="parentIDDropDownList" runat="server" DataSourceID="SqlDataSource3" DataTextField="name" DataValueField="ID"></asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
2
7920
by: Dabbler | last post by:
In my Registrant FormView I have a DropDownList which loads data from a secondary SqlDataSource "sdsOfficeParks". I need the user to select an office park but save the selected value in the FormView's ObjectDataSource "odsRegistrant" . The following solution can't work because I need the ddl selected value bound to my odsRegistrant but both tables have the same column name for primary key. <asp:DropDownList ID="ddlOfficePark"...
3
2499
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr = DataReader(sSQLcmd) Me.DropDownList1.DataSource = dr
1
1752
by: John | last post by:
If you set DropDownList.SelectedValue to an item not in the list, shouldn't an exception be thrown? ie: <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> <asp:ListItem Text="3" Value="3"></asp:ListItem> </asp:DropDownList> Then in the codebehind:
11
5823
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
7768
by: mitchman10 | last post by:
My Time table has TimeID,Employee,PayPeriod,ChargeCodeID,Hours My Chargecode table has ChargecodeID,c_Text I need an Editable datagrid that will show the TimeID,Employee,PayPeriod,C_Text in a label and hours, along with an edit linkbutton. When they click on edit, the edit button turns to Update Cancel TimeID stays read Only
1
16856
by: JJ | last post by:
Hi. I am having trouble getting a dropdownlist to work properly in a detailsview: The code is something like: <asp:DetailsView ID="dvwSubscriber" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="objCurrentSubscriber" DefaultMode="Insert" HeaderText="Subscriber Details">
2
9967
by: sree reddy | last post by:
..cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
0
10921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10601
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10658
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10300
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7023
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4072
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.