Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "'ddlAssignedTo' has a SelectedValue which is invalid because it does not exist in the list of items

I load the gridview and the gridview has an edit and delete buttons.

I click on Edit and I get, "ddlAssignedTo' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

I know that I am getting this error because the value fo ddlAssignedTo is null - nothing exists on the db for ddlAssignedTo.

All I was trying to do is update the current value.

So, my issue is this, if the current value is null, how do I assign a default value for ddlAssignedTo so that if no value currently exists on the db, the default value will prevail?

Here are some code:

Markup:

<asp:TemplateField HeaderText="Assigned To">
    <EditItemTemplate>
      <asp:DropDownList ID="ddlAssignedTo" runat="server" 
                        DataSourceID="SubjectDataSource"
                        DataTextField="fullname" DataValueField="empl_Id"
                        SelectedValue='<%# Bind("AssignedTo") %>'>
        <asp:ListItem Value="">--Select Name--</asp:ListItem>
      </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblAssigned" runat="server" 
                   Text='<% #Bind("fullname") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                   SelectCommand="SELECT Distinct [rownum],[reqnum], AssignedTo, (empl_first + ' ' + empl_last) fullname, [reqrecdate], [reqrecfrom], [skillsets], [application], [hoursperweek], [fromdate], [todate], [status], [statusupdate], [statusupby] FROM [requestinfo] left join employee on requestInfo.AssignedTo=employee.empl_id ORDER BY [reqnum]" 
                   UpdateCommand="INSERT INTO [requestinfo] ([reqnum], [reqrecdate], [reqrecfrom], [skillsets], [application], [hoursperweek], [fromdate], [todate], [status], [statusupdate], [statusupby],[AssignedTo]) VALUES (@reqnum, @reqrecdate, @reqrecfrom, @skillsets, @application, @hoursperweek, @fromdate, @todate, @status, @statusupdate, @statusupby,@empl_id)">
    <DeleteParameters>
        <asp:Parameter Name="rownum" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="reqnum" Type="String" />
        <asp:Parameter DbType="DateTime" Name="reqrecdate" />
        <asp:Parameter Name="reqrecfrom" Type="String" />
        <asp:Parameter Name="skillsets" Type="String" />
        <asp:Parameter Name="application" Type="String" />
        <asp:Parameter Name="hoursperweek" Type="Int32" />
        <asp:Parameter DbType="DateTime" Name="fromdate" />
        <asp:Parameter DbType="DateTime" Name="todate" />
        <asp:Parameter Name="status" Type="String" />
        <asp:Parameter DbType="DateTime" Name="statusupdate" />
        <asp:Parameter Name="statusupby" Type="String" />
        <asp:Parameter Name="empl_id" Type="String" />
        <asp:Parameter Name="rownum" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SubjectDataSource" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                   SelectCommand="SELECT empl_id,  (empl_first + ' ' + empl_last) fullname FROM dbo.Employee order by empl_last">
</asp:SqlDataSource>

CodeBehind:

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    Dim dd As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlstatus"), DropDownList)
    e.NewValues("status") = dd.SelectedItem.Text
    Dim ddAssigned As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlAssignedTo"), DropDownList)

    If String.IsNullOrEmpty(ddAssigned.SelectedValue) Then
        ddAssigned.SelectedValue = "shhhh"
    Else
        e.NewValues("empl_id") = ddAssigned.SelectedValue
    End If

    SqlDataSource1.DataBind()
End Sub
like image 975
Kenny Avatar asked May 12 '13 00:05

Kenny


2 Answers

Take a look at the solution provided by @cosmin.onea in the question 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items

This solution sets AppendDataBoundItems="true" on the DropDownList and creates one nullable ListItem so that your DropDownList will bind even when the field in the table is null.

Applied to the OP's problem, the following snippet would provide an option that would be selected when the AssignedTo field is null.

<asp:DropDownList ID="ddlAssignedTo" runat="server" 
    DataSourceID="SubjectDataSource" 
    DataTextField="fullname" DataValueField="empl_Id" 
    SelectedValue='<%# Bind("AssignedTo") %>' 
    AppendDataBoundItems="true">
        <asp:ListItem Text="Select" Value="" />
</asp:DropDownList>
like image 55
CameronM Avatar answered Nov 11 '22 19:11

CameronM


This solution sets AppendDataBoundItems="true" on the DropDownList and creates one nullable ListItem so that your DropDownList will bind even when the field in the table is null.

Applied to the OP's problem, the following snippet would provide an option that would be selected when the AssignedTo field is null.

<asp:DropDownList ID="ddlAssignedTo" runat="server" 
    DataSourceID="SubjectDataSource" 
    DataTextField="fullname" DataValueField="empl_Id" 
    SelectedValue='<%# Bind("AssignedTo") %>' 
    AppendDataBoundItems="true">
        <asp:ListItem Text="Select" Value="" />
</asp:DropDownList>

In this solution you can have a problem when saving if the user doesnt select a valid item. So you can also set

<asp:ListItem Text="Select" Value="" />

as Enabled = false Then when you open a new item it will bring a item that exists in the database.

I had the same problem and it solved to me.

like image 35
user3093469 Avatar answered Nov 11 '22 19:11

user3093469