7

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
4

4 回答 4

19

看看@cosmin.onea 在问题“DropDownList1”中提供的解决方案有一个无效的 SelectedValue,因为它不存在于项目列表中

此解决方案设置AppendDataBoundItems="true"DropDownList创建一个nullableListItem,以便DropDownList即使表中的字段为空,您也将绑定。

应用于 OP 的问题,以下代码段将提供一个选项,当 AssignedTo 字段为空时将选择该选项。

<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>
于 2013-06-12T10:29:44.867 回答
1

此解决方案在 DropDownList 上设置 AppendDataBoundItems="true" 并创建一个可为空的 ListItem,以便即使表中的字段为空,您的 DropDownList 也会绑定。

应用于 OP 的问题,以下代码段将提供一个选项,当 AssignedTo 字段为空时将选择该选项。

<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>

在此解决方案中,如果用户未选择有效项目,则保存时可能会出现问题。所以你也可以设置

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

as Enabled = false 然后,当您打开一个新项目时,它将带来一个存在于数据库中的项目。

我有同样的问题,它解决了我。

于 2015-07-28T19:01:07.220 回答
0

嗨,我检查了同样的问题。我的麻烦是,数据字段的索引号。我正在使用像这个表[3]这样的数据字段索引号,并且在创建之后gridview,我更改了数据库表的顺序。

毕竟我使用了字段名称而不是字段索引号,问题就解决了。

于 2013-11-08T17:35:29.913 回答
0

您在命令 SQL Server 中的问题,您必须在事件激活时将 id 与下拉列表的 id 当前选择匹配,然后问题就解决了。

  1. 再次查看包含两个表的键以便您查询。

  2. Bin("Name") 必须与命令查询中的名称匹配。

于 2014-04-13T02:27:45.810 回答