Error: SelectedValue, which is invalid because it does not exist in the list of items

I have a Gridview that binds to an ObjectDataSource object (objStudentDetails). In the edit / paste mode of Gridview, one of the fields is DropDownList, which gets it from the list of the list from the search list. I have a DropDownList binding to another ObjectDataSource control (objStateList) that represents a lookup table. It works fine as long as the value in objStudentDetails ObjectDataSource matches one of the values ​​in objStateList ObjectDataSource, at least in the case of a non-empty string value anyway.

objStateList has these values ​​(from a stored proc that loads it - ID # 6 - empty string ''):

StateId     State
----------- -----
6             
4           AL
1           GA
3           KY
2           TN

objStudentDetails has these values ​​(from a stored proc that loads it):

FirstName   LastName   State
----------- ---------- -----
tone        smith      TN

Or it may have this result set (State - empty string - ''):

FirstName   LastName   State
----------- ---------- -----
jenny       johnson     

In the first objStudentDetails result set, the state of the DropDownList in the EditItemTemplate is displayed in order. However, in the second result set, I get this error:

'ddlEditState' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 

I would think that since my lookup table has a value with an empty string, the value of objStudentDetails with an empty string for the state will match, but something does not work as I expect.

Here is my EditItemTemplate code from Gridview:

<EditItemTemplate>
  <asp:Panel ID="panEditState" runat="server">
    <asp:DropDownList ID="ddlEditState" runat="server" CssClass="GridviewDropdownlist"
      DataSourceID="objStateList" DataTextField="State" DataValueField="State"      
      SelectedValue='<%# Bind("State") %>'
      Width="50px">
</asp:DropDownList>
</asp:Panel>
</EditItemTemplate>

And objStateList, which calls the method that passes the parameter from which the lookup table is requested:

<asp:ObjectDataSource ID="objStateList" runat="server" SelectMethod="GetDropdownData"     TypeName="AIMLibrary.BLL.DropdownData">
<SelectParameters>
<asp:Parameter Name="itemsToGet" DefaultValue="state" />
</SelectParameters>
</asp:ObjectDataSource>

Any ideas?

+5
source share
4 answers

AppendDataBoundItems DropDownLists true. NULL ListItem, DropDownList <asp:ListItem>, :

<asp:DropDownList ID="Categories" runat="server"
    DataSourceID="CategoriesDataSource" DataTextField="CategoryName"
    DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'
    AppendDataBoundItems="True">
    <asp:ListItem Value="">[nothing selected]</asp:ListItem>
</asp:DropDownList>
+9

, , . , . objectdatasource, selectedvalue () gridview.

, , . gridview , . , , .

, , . gridview ( ) . , , , gridview.

. Page_Load

+1

AppendDataBoundItems = "True" > , . dropdownlist GridView - , Microsoft . , ASP PHP. , .

0

, , , : - , .

  • : DDL AppendDataBoundItem=true (.. ", " ):

    < asp: DropDownList ID = "DropDownList5 runat =" "AppendDataBoundItems =" True "... > < asp: ListItem > </: ListItem >                           </: DropDownList >

, 80% . , ( ) , DDL, - Query - SELECT ID, Name from EMPLOYEES where Department =@Department, @Department "" "" "- " "DDL .

  1. : DDL GridView_RowDataBound (fount

( - )

    protected void GridView5_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    //********** this  is a workaround for the annoying problem with dropdownlist in gidview without adding new item ************
    if (e.Row.RowType == DataControlRowType.DataRow && GridView5.EditIndex == e.Row.RowIndex)
    {
        DropDownList DropDownList5 = (DropDownList)e.Row.FindControl("DropDownList5");
        string query = "SELECT gkey as empID, name FROM [employees] where department=@department";
        SqlCommand command = new SqlCommand(query);
        command.Parameters.AddWithValue("@department", lblDepartment.Text);
        DropDownList5.DataSource = GetData(command);
        DropDownList5.DataTextField = "name";
        DropDownList5.DataValueField = "empID";
        DropDownList5.DataBind();
    }

GetData:

   private DataTable GetData (SqlCommand cmd)
{
    string strConnString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt= new DataTable())
            {
                sda.Fill(dt);
                return dt;
            }
        }
    }
}
0

All Articles