|
|
|
|
Google Adsense |
|
|
|
Modules |
|
|
|
New at Group29 |
|
|
|
TheForce.Net |
|
|
|
Hot trends |
|
|
|
|
DropDownList has a SelectedValue which is invalid because it does not exist ...
|
|
|
Here is the scenario, I was using a gridview
control in visual studio 2005. I had an itemtemplate with a label for
the value and an edititemtemplate with a dropdownlist to use to select
values. Unfortunately, the actual row in the main table did not have a
corresponding entry in the source table for the drop down list. So I
got an error:
System.ArgumentOutOfRangeException: DropDownList1 has a SelectedValue
which is invalid because it does not exist in the list of items.
This is one of the popular hits on the Group29 Tech tips forum.
Group29 Tech Tips forum: DropDownList has a SelectedValue which is invalid because it does not exist in the list of items.
This is quite a problem if you have not maintained data integrity, and/or if your Select statment in your dropdownlist data source does not include particular values.
My problem was that my main table had a key in the product manager field for a user in the user table who was not flagged as a product manager.
After much scratching and fumbling, I eventually decided on two possibilities.
Plan A.)
1.) In the RowEditing event, grab the current person from the list and store in a private member variable
Code:
Dim WhichRow As Integer = e.NewEditIndex
m_SelectedManager = CType(CType(grvProduct.Rows(WhichRow).Cells(4).Controls.Item(1), System.Web.UI.Control), System.Web.UI.WebControls.Label).Text()
2.) Then use that member variable in a SQL statement to look up the corresponding ID.
3.) Find the drop down list in the Gridview databinding event and insert the missing item. The rowstate for the edited row will be edit (4) or alternate(1) + edit(4) = 5
Code:
If ((e.Row.RowState = 4) Or (e.Row.RowState = 5)) Then
Dim MydrpEditMgrId As DropDownList = CType(e.Row.Cells(4).FindControl("drpEditMgrId"), DropDownList)
Dim myUserID As Integer = GetUserIDbyName(m_SelectedManager)
Dim myListItem = New ListItem(m_SelectedManager & " *", myUserID)
Try
MydrpEditMgrId.Items.Add(myListItem)
Catch
End Try
Try
MydrpEditMgrId.SelectedValue = myUserID
Catch
End Try
End If
Plan B.)
This turned out to be the final solution, because the user was interested in having all the users available. Change the SELECT statement to be a UN-ION of both the source table and the list of all the entries in the main table that would not have appeared in the source table.
Here is one example that does not use a UN-ION (dash to protect for PHP-Nuke) query, but instead a SELECT IN query.
asp:SqlDataSource ID="DropDownListProjectManager2" runat="server" ConnectionString="<%$ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT [usrName], [usrId] FROM [the_usr_table] WHERE (( ([usrDeleteFlag] = @usrDeleteFlag) AND ([usrStatus] = @usrStatus)) OR ([usrID] in (SELECT [prjdevid] from the_prj_table where prjid = @prjid ))) ORDER BY [usrName]">
/asp:SqlDataSource
In this example, we were getting an error because the user status of the current user in the user table did not match the required user status to be in the project table. The rule was that a user could be assigned as a project manager on the project even if the status had changed.
|
|
|
|
Posted on Wednesday, September 12, 2007 @ 08:57:40 UTC by BB
|
|
|
|
"DropDownList has a SelectedValue which is invalid because it does not exist ..." | Login/Create an Account | 0 comments |
| The comments are owned by the poster. We aren't responsible for their content. |
|
|
|
|
|
No Comments Allowed for Anonymous, please register |
|
|
|
|
|
:: HeliusGray phpbb2 style by CyberAlien :: PHP-Nuke theme by www.nukemods.com ::
| |