【问题标题】:System.Data.DataRowView' does not contain a property with the name 'State'System.Data.DataRowView' 不包含名为 'State' 的属性
【发布时间】:2016-08-08 22:19:59
【问题描述】:
<asp:GridView ID="dgvCustomers" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center" DataKeyNames="ID" DataSourceID="dsCustomerTickets" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnRowUpdated="dgvCustomers_RowUpdated">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField EditImageUrl="~/svg/edit.png" UpdateImageUrl="~/svg/save.png" CancelImageUrl="~/svg/cancel.png" ButtonType="Image" ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:BoundField DataField="System" HeaderText="System" SortExpression="System" />
                <asp:BoundField DataField="LoggedDate" HeaderText="LoggedDate" SortExpression="LoggedDate" />
                <asp:BoundField DataField="ClosedDate" HeaderText="ClosedDate" SortExpression="ClosedDate" />
                <asp:BoundField DataField="Resolution" HeaderText="Resolution" SortExpression="Resolution" />
                <%--<asp:BoundField DataField="HistoryID" HeaderText="HistoryID" SortExpression="HistoryID" />--%>
                <%--<asp:BoundField DataField="Priority" HeaderText="Priority" SortExpression="Priority" />--%>
                <asp:BoundField DataField="LoggedBy" HeaderText="LoggedBy" SortExpression="LoggedBy" />
                <asp:TemplateField HeaderText="State" SortExpression="State2">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlstate" runat="server" DataSourceID="SqlDataSource2" DataTextField="State" DataValueField="ID" SelectedValue='<%# Bind("State") %>' AppendDataBoundItems="true">
                            <asp:ListItem Text="Select a State" Value="" />
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("State2") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Priority" SortExpression="State3">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlpriority" runat="server" DataSourceID="SqlDataSource3" DataTextField="Priority" DataValueField="ID" SelectedValue='<%# Bind("Priority") %>' AppendDataBoundItems="true">
                            <asp:ListItem Text="Select Priority" Value="" />
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("State3") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#0099ff" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FBFBF2" />
            <SortedAscendingHeaderStyle BackColor="#848384" />
            <SortedDescendingCellStyle BackColor="#EAEAD3" />
            <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>
        <asp:SqlDataSource ID="dsCustomerTickets" runat="server" ConnectionString="<%$ ConnectionStrings:TicketsSupportConnectionString %>" DeleteCommand="DELETE FROM [Tickets] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Tickets] ([Title], [Description], [System], [LoggedDate], [ClosedDate], [Resolution], [Priority], [HistoryID], [LoggedBy], [State]) VALUES (@Title, @Description, @System, @LoggedDate, @ClosedDate, @Resolution, @Priority, @HistoryID, @LoggedBy, @State)" SelectCommand="SELECT T.ID, T.Title, T.Description, T.System, T.Attachments, T.LoggedDate, T.ClosedDate, T.Resolution, T.Priority, T.LoggedBy, S.State AS State2, p.Priority AS State3 FROM Tickets AS T INNER JOIN State AS S ON T.State = S.ID INNER JOIN Priority AS p ON T.Priority = p.ID WHERE (T.LoggedBy = @LoggedBy)" UpdateCommand="UPDATE Tickets SET Description = @Description, System = @System, ClosedDate = @ClosedDate, Resolution = @Resolution, Priority = @Priority, HistoryID = @HistoryID, State = @State WHERE (ID = @ID)">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="System" Type="String" />
                <asp:Parameter Name="LoggedDate" Type="DateTime" />
                <asp:Parameter Name="ClosedDate" Type="DateTime" />
                <asp:Parameter Name="Resolution" Type="String" />
                <asp:Parameter Name="Priority" Type="Int32" />
                <asp:Parameter Name="HistoryID" Type="Int32" />
                <asp:Parameter Name="LoggedBy" Type="String" />
                <asp:Parameter Name="State" Type="Int32" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="LoggedBy" QueryStringField="u" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="System" Type="String" />
                <asp:Parameter Name="ClosedDate" Type="DateTime" />
                <asp:Parameter Name="Resolution" Type="String" />
                <asp:Parameter Name="Priority" Type="Int32" />
                <asp:Parameter Name="HistoryID" Type="Int32" />
                <asp:Parameter Name="State" Type="Int32" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TicketsSupportConnectionString %>" SelectCommand="SELECT [ID],[State] FROM [dbo].[State]"></asp:SqlDataSource>

使用上面的代码,我不断收到错误消息。 在 SQL 中,如果我运行 SqlDataSource2 查询,我会得到结果(ID 和状态)。

我尝试在查询生成器中更改查询并编辑拖动的 SqlDataSource2。 一旦我单击命令字段“EditImageUrl”,我就会收到上述错误。 也许我在看我的代码很长时间才注意到任何问题。

我用谷歌搜索了它,但没有看到以下内容: 我看过Here 和这里一样,我在下拉列表中使用了 bind 而不是 Eval。

我也尝试将我的 sqldatasource 更改为:

  1. 从州中选择 *
  2. 从州中选择 ID、州
  3. 从 dbo.State 中选择 ID、State

任何帮助将不胜感激。

【问题讨论】:

    标签: c# sql asp.net gridview dropdown


    【解决方案1】:

    编辑和更新

    EditTemplate 中,您使用正确的DataTextField="State" DataValueField="ID" 填写SqlDataSource2 中的aspDropDown。但是SelectedValue 属于&lt;asp:SqlDataSource ID="dsCustomerTickets"。所以你需要T.State in SelectCommand 才能使绑定SelectedValue='&lt;%# Bind("State") %&gt;' 正确。在Bind 中也不需要ItemTemplateEval("State2") 就够了。
    第一个SqlDataSource 应该是

        <asp:SqlDataSource ID="dsCustomerTickets" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TicketsSupportConnectionString %>" 
            DeleteCommand="DELETE FROM [Tickets] WHERE [ID] = @ID" 
            InsertCommand="INSERT INTO [Tickets] (/*removed for shortness*/ [State]) VALUES ( @State2)" 
            SelectCommand="SELECT /*your list*/ T.State, S.State AS State2, p.Priority AS State3 FROM Tickets AS T INNER JOIN State AS S ON T.State = S.ID INNER JOIN Priority AS p ON T.Priority = p.ID WHERE (T.LoggedBy = @LoggedBy)" 
            UpdateCommand="UPDATE Tickets SET /*removed for shortness*/ State = @State2 WHERE (ID = @ID)">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <%--your parameters removed for shortness--%>
                <asp:Parameter Name="State" Type="Int32" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="LoggedBy" QueryStringField="u" />
            </SelectParameters>
            <UpdateParameters>
                <%--your parameters removed for shortness--%>
                <asp:Parameter Name="State" Type="Int32" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多