System.Data.DataRowView'不包含名称为' State'的属性

时间:2016-08-08 22:20:00

标签: c# sql asp.net gridview dropdown

<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和State)。 SQL Results

我尝试在查询构建器中更改查询并编辑拖动的SqlDataSource2。 只要我点击命令字段&#34; EditImageUrl&#34;我得到了上述错误。 也许我正在查看我的代码,以便长时间注意任何问题。

我用google搜索它没有运气看起来像: 我看过Here 在这里,我在下拉列表中绑定而不是Eval。

我也尝试将sqldatasource更改为:

  1. 从州选择*
  2. 选择ID,状态州
  3. 从dbo.State
  4. 中选择ID,状态

    任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

编辑和更新

EditTemplate中,您使用正确aspDropDownSqlDataSource2填写DataTextField="State" DataValueField="ID"。但SelectedValue属于<asp:SqlDataSource ID="dsCustomerTickets"。因此,T.State中需要SelectCommand才能使绑定SelectedValue='<%# Bind("State") %>'正确无误。 Bind中的ItemTemplate也不需要。 Eval("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>