DropDownList has SelectedValue which in invalid

B

Brett

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
 
A

Allen Chen [MSFT]

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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Brett" <[email protected]>
| 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: <[email protected]>
| 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!TK2MSFTNGP02.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
|
|
|
 
B

Brett

Allen,

Thank you for your response. The DropDownList is bound to the field in its
definition, so the error occurs as soon as I load the page. I have included
some code below to illustrate.

aspx:
<asp:SqlDataSource ID="TechnicianSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:SSR_ConnString %>"
DataSourceMode="DataReader"
SelectCommand="SELECT [Name] FROM [Technician] WHERE ([Active] = 1) ORDER
BY Indx">
</asp:SqlDataSource>

<asp:DropDownList ID="ddlAssignedTo" runat="server"
DataSourceID="TechnicianSQLDataSource"
DataTextField="Name"
DataValueField="Name"
SelectedValue='<%# Bind("ResolvedBy") %>'
AppendDataBoundItems="True">
<asp:ListItem Selected="True"> </asp:ListItem>
</asp:DropDownList>

aspx.vb:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If My.User.IsInRole(Domain & "\Maint_Full") Then
FormView1.DefaultMode = FormViewMode.Edit
Else
FormView1.DefaultMode = FormViewMode.ReadOnly
End If

'I tried the following code in the PreRender event, but the error
'occurred before PreRender. So, I moved it to Page_Load,
'but I still get the "SelectedValue invalid" error when ddl1 is
'declared below.
If FormView1.CurrentMode = FormViewMode.Edit Then
Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
Dim ddl1 As DropDownList =
CType(FormView1.FindControl("ddlAssignedTo"), DropDownList)
If Not IsNothing(rowView) AndAlso Not
IsNothing(ddl1.Items.FindByValue(rowView("ResolvedBy").ToString())) Then
ddl1.SelectedValue = rowView("ResolvedBy").ToString()
Else
ddl1.Items.Add(rowView("ResolvedBy").ToString())
ddl1.SelectedValue = rowView("ResolvedBy").ToString()
End If
End If

If FormView1.CurrentMode = FormViewMode.Edit Then
FormView1.FindControl("ddlAssignedTo").Focus()
End If
End Sub


Thanks again,
Brett


Allen Chen said:
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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
| From: "Brett" <[email protected]>
| 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: <[email protected]>
| 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!TK2MSFTNGP02.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
|
|
|
 
B

Brett

I believe I have found a solution by inserting the missing DropDownList item
from the FormView's ItemCreated event procedure. This inserts the item
before the databound items are appended. So, to prevent duplicates, I have
to make sure not to add any items that will be appended after databind.
The code is below. This may not be the most efficient way to do things,
but it is one way. Alternatively, I could remove two-way databinding
and populate the entire ddl itemlist in code.

Just a note: I also tried inserting the item in the DataBound event, but
this occurs too late, resulting in the invalid SelectedValue error.

Code in aspx.vb:
Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles FormView1.ItemCreated

If FormView1.CurrentMode = FormViewMode.Edit Then
Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
Dim ddl1 As DropDownList = CType(FormView1.FindControl("ddlAssignedTo"),
DropDownList)

If Not IsNothing(rowView) Then
'Fill a string with a list of active items
Dim args As New DataSourceSelectArguments
ItemSqlDataSource.DataSourceMode = SqlDataSourceMode.DataSet
Dim view As DataView = ItemSqlDataSource.Select(args)
Dim table As DataTable = view.ToTable
Dim sActiveItems As String = ""
For Each dr As DataRow In table.Rows
sActiveItems = sActiveItems & dr.ItemArray(0).ToString & "|"
Next

'If the record was originally saved with an item that is no longer
'active, add it to the ddl and make it the selected item
If Not (sActiveItems.Contains(rowView("ResolvedBy"))) Then
ddl1.Items.Add(rowView("ResolvedBy").ToString())
ddl1.SelectedValue = rowView("ResolvedBy").ToString()
End If
End If
End If
End Sub




Brett said:
Allen,

Thank you for your response. The DropDownList is bound to the field in
its
definition, so the error occurs as soon as I load the page. I have
included
some code below to illustrate.

aspx:
<asp:SqlDataSource ID="TechnicianSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:SSR_ConnString %>"
DataSourceMode="DataReader"
SelectCommand="SELECT [Name] FROM [Technician] WHERE ([Active] = 1) ORDER
BY Indx">
</asp:SqlDataSource>

<asp:DropDownList ID="ddlAssignedTo" runat="server"
DataSourceID="TechnicianSQLDataSource"
DataTextField="Name"
DataValueField="Name"
SelectedValue='<%# Bind("ResolvedBy") %>'
AppendDataBoundItems="True">
<asp:ListItem Selected="True"> </asp:ListItem>
</asp:DropDownList>

aspx.vb:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If My.User.IsInRole(Domain & "\Maint_Full") Then
FormView1.DefaultMode = FormViewMode.Edit
Else
FormView1.DefaultMode = FormViewMode.ReadOnly
End If

'I tried the following code in the PreRender event, but the error
'occurred before PreRender. So, I moved it to Page_Load,
'but I still get the "SelectedValue invalid" error when ddl1 is
'declared below.
If FormView1.CurrentMode = FormViewMode.Edit Then
Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
Dim ddl1 As DropDownList =
CType(FormView1.FindControl("ddlAssignedTo"), DropDownList)
If Not IsNothing(rowView) AndAlso Not
IsNothing(ddl1.Items.FindByValue(rowView("ResolvedBy").ToString())) Then
ddl1.SelectedValue = rowView("ResolvedBy").ToString()
Else
ddl1.Items.Add(rowView("ResolvedBy").ToString())
ddl1.SelectedValue = rowView("ResolvedBy").ToString()
End If
End If

If FormView1.CurrentMode = FormViewMode.Edit Then
FormView1.FindControl("ddlAssignedTo").Focus()
End If
End Sub


Thanks again,
Brett


Allen Chen said:
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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
| From: "Brett" <[email protected]>
| 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: <[email protected]>
| 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!TK2MSFTNGP02.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
|
|
|
 
A

Allen Chen [MSFT]

Hi Brett,

Glad to know this issue has been solved. If you need further assistance,
please don't hesitate to let me know.

Regards,
Allen Chen
Microsoft Online Support
--------------------
| From: "Brett" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
| Subject: Re: DropDownList has SelectedValue which in invalid
| Date: Mon, 22 Sep 2008 13:23:49 -0400
| Lines: 316
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
| Message-ID: <[email protected]>
| 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!TK2MSFTNGP04.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:76534
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I believe I have found a solution by inserting the missing DropDownList
item
| from the FormView's ItemCreated event procedure. This inserts the item
| before the databound items are appended. So, to prevent duplicates, I
have
| to make sure not to add any items that will be appended after databind.
| The code is below. This may not be the most efficient way to do things,
| but it is one way. Alternatively, I could remove two-way databinding
| and populate the entire ddl itemlist in code.
|
| Just a note: I also tried inserting the item in the DataBound event, but
| this occurs too late, resulting in the invalid SelectedValue error.
|
| Code in aspx.vb:
| Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As
| System.EventArgs) Handles FormView1.ItemCreated
|
| If FormView1.CurrentMode = FormViewMode.Edit Then
| Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
| Dim ddl1 As DropDownList =
CType(FormView1.FindControl("ddlAssignedTo"),
| DropDownList)
|
| If Not IsNothing(rowView) Then
| 'Fill a string with a list of active items
| Dim args As New DataSourceSelectArguments
| ItemSqlDataSource.DataSourceMode = SqlDataSourceMode.DataSet
| Dim view As DataView = ItemSqlDataSource.Select(args)
| Dim table As DataTable = view.ToTable
| Dim sActiveItems As String = ""
| For Each dr As DataRow In table.Rows
| sActiveItems = sActiveItems & dr.ItemArray(0).ToString & "|"
| Next
|
| 'If the record was originally saved with an item that is no longer
| 'active, add it to the ddl and make it the selected item
| If Not (sActiveItems.Contains(rowView("ResolvedBy"))) Then
| ddl1.Items.Add(rowView("ResolvedBy").ToString())
| ddl1.SelectedValue = rowView("ResolvedBy").ToString()
| End If
| End If
| End If
| End Sub
|
|
|
|
| | > Allen,
| >
| > Thank you for your response. The DropDownList is bound to the field in
| > its
| > definition, so the error occurs as soon as I load the page. I have
| > included
| > some code below to illustrate.
| >
| > aspx:
| > <asp:SqlDataSource ID="TechnicianSqlDataSource" runat="server"
| > ConnectionString="<%$ ConnectionStrings:SSR_ConnString %>"
| > DataSourceMode="DataReader"
| > SelectCommand="SELECT [Name] FROM [Technician] WHERE ([Active] = 1)
ORDER
| > BY Indx">
| > </asp:SqlDataSource>
| >
| > <asp:DropDownList ID="ddlAssignedTo" runat="server"
| > DataSourceID="TechnicianSQLDataSource"
| > DataTextField="Name"
| > DataValueField="Name"
| > SelectedValue='<%# Bind("ResolvedBy") %>'
| > AppendDataBoundItems="True">
| > <asp:ListItem Selected="True"> </asp:ListItem>
| > </asp:DropDownList>
| >
| > aspx.vb:
| > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
| > System.EventArgs) Handles Me.Load
| >
| > If My.User.IsInRole(Domain & "\Maint_Full") Then
| > FormView1.DefaultMode = FormViewMode.Edit
| > Else
| > FormView1.DefaultMode = FormViewMode.ReadOnly
| > End If
| >
| > 'I tried the following code in the PreRender event, but the error
| > 'occurred before PreRender. So, I moved it to Page_Load,
| > 'but I still get the "SelectedValue invalid" error when ddl1 is
| > 'declared below.
| > If FormView1.CurrentMode = FormViewMode.Edit Then
| > Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
| > Dim ddl1 As DropDownList =
| > CType(FormView1.FindControl("ddlAssignedTo"), DropDownList)
| > If Not IsNothing(rowView) AndAlso Not
| > IsNothing(ddl1.Items.FindByValue(rowView("ResolvedBy").ToString())) Then
| > ddl1.SelectedValue = rowView("ResolvedBy").ToString()
| > Else
| > ddl1.Items.Add(rowView("ResolvedBy").ToString())
| > ddl1.SelectedValue = rowView("ResolvedBy").ToString()
| > End If
| > End If
| >
| > If FormView1.CurrentMode = FormViewMode.Edit Then
| > FormView1.FindControl("ddlAssignedTo").Focus()
| > End If
| > End Sub
| >
| >
| > Thanks again,
| > Brett
| >
| >
| > | >> 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:
| >> (e-mail address removed).
| >>
| >> ==================================================
| >> Get notification to my posts through email? Please refer to
| >>
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/default.aspx?target=assistance&ln=en-us.
| >> ==================================================
| >> This posting is provided "AS IS" with no warranties, and confers no
| >> rights.
| >>
| >> --------------------
| >> | From: "Brett" <[email protected]>
| >> | 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: <[email protected]>
| >> | 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!TK2MSFTNGP02.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
| >> |
| >> |
| >> |
| >>
| >
| >
| >
| >
| >
| >
| >
| >
| >
| >
|
|
|
|
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top