473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4899
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.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Uncomment following code to reproduce this exception
// this.DropDownList1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
this.DropDownList1.SelectedValue = this.TextBox1.Text;
}
}
aspx:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
SelectCommand="SELECT * FROM [Table_1]"></asp:SqlDataSource>

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="theID"
DataValueField="theID">
</asp:DropDownList>

<asp:Button ID="Button1" runat="server" onclick="Button1_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_PreRender(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
ddl.Items.Insert(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****@microsoft.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*****@newsgroup.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.public.dotnet.framework.aspnet
| NNTP-Posting-Host: dsl-1-210.d01.scpnbh.pbtcomm.net 64.53.27.210
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSF TNGP02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:76417
| X-Tomcat-NG: microsoft.public.dotnet.framework.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
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...
15
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"...
2
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...
3
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...
1
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"...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
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...
1
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" ...
2
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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,...

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.