数据绑定期间集合修改错误

本文关键字:修改 错误 集合 数据绑定 | 更新日期: 2023-09-27 17:56:43

在"/"应用程序中收到服务器错误。

The control collection cannot be modified during Databind, Init, Load, PreRender or Unload phases.
    Line 113:  if (cbShowDiscontinued.Checked)
    Line 114:  {
    Line 115:      gvProducts.DataSourceID=dsDiscountinuedProducts.ID);
    Line 116:  }
    Line 117:  else

说错误在第 115 行,但我不明白为什么。 我尝试遍历所有SQL连接字符串,并确保所有必要的字符串

我尝试改变几件事,但无济于事。
下面是我的代码和代码隐藏。我们正在使用本地数据库。

    <%@ Page Title="WGProductsCRUD" Language="C#"     
    MasterPageFile="~/WF2Master.master" AutoEventWireup="true"     
    CodeFile="WGProductsCRUD.aspx.cs" Inherits="WGProductsCRUD" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="cphHead" runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphMain" runat="Server">
    <h2>WG Products CRUD</h2>

    <asp:MultiView ID="mvProducts" runat="server" ActiveViewIndex="0">
        <asp:View ID="vwMaster" runat="server">
    <asp:GridView ID="gvProducts" runat="server" AllowPaging="True"   
    AllowSorting="True" AutoGenerateColumns="False"   
    DataSourceID="dsActiveProducts" DataKeyNames="ProdId">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="ProdName" HeaderText="Products" 
    SortExpression="ProdName" />
            <asp:BoundField DataField="Price" DataFormatString="{0:c}" 
    HeaderText="Price" SortExpression="Price" />
            <asp:BoundField DataField="ProductStatusName" HeaderText="Status" 
    SortExpression="ProductStatusName" />
            <asp:BoundField DataField="CategoryName" HeaderText="Category"     
    SortExpression="CategoryName" />
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server"     
    CausesValidation="False" CommandName="Delete" OnClientClick="return  
    confirm('Are you sure?')" OnPreRender="cbShowDiscontinued_CheckedChanged" 
    Text='<%# Eval("ButtonText") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dsActiveProducts" runat="server"
        ConnectionString="<%$ ConnectionStrings:WGConString %>"
        DeleteCommand="UPDATE [WGProducts] SET ProdStatusId=4 WHERE [ProdId]   
    = 
    @ProdId"
        InsertCommand="INSERT INTO [WGProducts] ([ProdName], [Price]) VALUES   
    (@ProdName, @Price)"
        SelectCommand="SELECT WGProducts.ProdId, WGProducts.ProdName, 
    WGProducts.Price, WGCategories.CategoryId, 
    WGProductStatuses.ProductStatusName, WGCategories.CategoryName,  
'Deactivate' 
    AS ButtonText FROM WGProducts LEFT OUTER JOIN WGCategories ON 
    WGProducts.ProdCategoryId = WGCategories.CategoryId LEFT OUTER JOIN 
    WGProductStatuses ON WGProducts.ProdStatusId = 
    WGProductStatuses.ProductStatusId WHERE (WGProducts.ProdStatusId &lt;   
    @ProdStatusId) ORDER BY WGProducts.ProdName"
        UpdateCommand="UPDATE [WGProducts] SET [ProdName] = @ProdName, 

[Price] = 
    @Price WHERE [ProdId] = @ProdId">
        <DeleteParameters>
            <asp:Parameter Name="ProdId" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ProdName" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
        </InsertParameters>
        <SelectParameters>
            <asp:Parameter DefaultValue="4" Name="ProdStatusId" Type="Int32" 
/>
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ProdName" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="ProdId" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="dsDiscontinuedProducts" runat="server"   
    ConnectionString="<%$ ConnectionStrings:WGConString %>"   
DeleteCommand="Update 
    [WGProducts] set ProdStatusId=1 WHERE [ProdId] = @ProdId"     
    InsertCommand="INSERT 
    INTO [WGProducts] ([ProdName], [Price]) VALUES (@ProdName, @Price)" 
    SelectCommand="SELECT WGProducts.ProdId, WGProducts.ProdName,     
    WGProducts.Price, 
    WGCategories.CategoryName, WGProductStatuses.ProductStatusName, 
'Reactivate' 
    AS 
    ButtonText FROM WGProducts LEFT OUTER JOIN WGProductStatuses ON 
    WGProducts.ProdStatusId = WGProductStatuses.ProductStatusId LEFT OUTER 
JOIN 
    WGCategories ON WGProducts.ProdCategoryId = WGCategories.CategoryId WHERE 
    (WGProducts.ProdStatusId = @ProdStatusId) ORDER BY WGProducts.ProdName" 
    UpdateCommand="UPDATE [WGProducts] SET [ProdName] = @ProdName, [Price] =     
    @Price 
    WHERE [ProdId] = @ProdId">
        <DeleteParameters>
            <asp:Parameter Name="ProdId" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ProdName" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
        </InsertParameters>
        <SelectParameters>
            <asp:Parameter DefaultValue="4" Name="ProdStatusId" Type="Int32" 
 />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ProdName" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="ProdId" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:LinkButton ID="btnAddProduct" runat="server" 
    OnClick="btnAddProduct_Click">Add A Product</asp:LinkButton>
        </asp:View>
        <asp:View ID="vwDetails" runat="server" EnableViewState="False">
    <asp:DetailsView ID="dvSelectedProduct" runat="server" Height="50px" 
    Width="494px" AutoGenerateRows="False" DataKeyNames="ProdId" 
    DataSourceID="dsSelectedProduct">
        <Fields>
            <asp:BoundField DataField="ProdId" HeaderText="Prod Id" 
    InsertVisible="False" ReadOnly="True" SortExpression="ProdId" />
            <asp:TemplateField HeaderText="Prod Name" 
SortExpression="ProdName">
                <EditItemTemplate>
                    <asp:TextBox ID="tbProductName" runat="server" Text='<%# 
    Bind("ProdName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvProduct" 
runat="server"
                        ControlToValidate="tbProductName"  
                        ErrorMessage="*Required" 
                        Font-Italic="True" 
                        ForeColor="Red"></asp:RequiredFieldValidator>
                    &nbsp;
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="tbProductName" runat="server" Text='<%# 
    Bind("ProdName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvProduct" 
 runat="server"
                        ControlToValidate="tbProductName"
                        ErrorMessage="*Required"
                        Font-Italic="True"
                        ForeColor="Red"></asp:RequiredFieldValidator>
                    &nbsp;
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# 
    Bind("ProdName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description" 
    SortExpression="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="tbDescription" runat="server"
                        Text='<%# Bind("Description") %>'
                        Height="88px"
                        TextMode="MultiLine"
                        Width="183px"></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="tbDescription" runat="server"
                        Text='<%# Bind("Description") %>'
                        Height="88px"
                        TextMode="MultiLine"
                        Width="183px"></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"
                        Text='<%# Bind("Description") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Price" DataFormatString="{0:c}" 
    HeaderText="Price" SortExpression="Price" />
            <asp:BoundField DataField="ModelNumber" HeaderText="Model Number" 
    SortExpression="ModelNumber" />
            <asp:TemplateField HeaderText="Prod Image" 
    SortExpression="ImageFileName">
                <EditItemTemplate>
                    <asp:FileUpload ID="fupProdImage" runat="server" />
                    <%--The hidden field will help us reset the original    
image 
    being used if the user doesn't change the image--%>
                    <asp:HiddenField ID="hfOriginalImageName" runat="server" 
    Value='<%# Bind("ImageFileName") %>' />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:FileUpload ID="fupProdImage" runat="server" />
                    <%--No hidden field needed here--%>
                </InsertItemTemplate>
                <ItemTemplate>
                    <%--Replaced this with custom-databound image control--%>
                    <%--                    <asp:Label ID="Label3" 
runat="server" 
    Text='<%# Bind("ImageFileName") %>'></asp:Label>--%>
                    <asp:Image ID="imgProductImage" runat="server"
                        Height="200px"
                        ImageUrl='<%# Eval("ImageFileName", 
    "~/Images/Products/{0}") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="LastUpdate"
                DataFormatString="{0:f}"
                HeaderText="Last Update"
                SortExpression="LastUpdate" />
            <asp:TemplateField HeaderText="Category Name"
                SortExpression="CategoryName">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlCategoryName" runat="server"
                        AppendDataBoundItems="True"
                        DataSourceID="dsCategories"
                        DataTextField="CategoryName"
                        DataValueField="CategoryId"
                        SelectedValue='<%# Bind("ProdCategoryId") %>'>
                        <asp:ListItem
                            Value="">[--Make a Selection--]</asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsCategories" runat="server"
                        ConnectionString="<%$ ConnectionStrings:WGConString     
%>"
                        SelectCommand="SELECT [CategoryId], [CategoryName]  
FROM 
    [WGCategories]"></asp:SqlDataSource>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="ddlCategoryName" runat="server"
                        AppendDataBoundItems="True"
                        DataSourceID="dsSelectedProduct"
                        DataTextField="CategoryName"
                        DataValueField="CategoryId"
                        SelectedValue='<%# Bind("ProdCategoryId") %>'>
                        <asp:ListItem
                            Value="">[--Make a Selection--]</asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsCategories" runat="server"
                        ConnectionString="<%$ ConnectionStrings:WGConString  
%>"
                        SelectCommand="SELECT [CategoryId], [CategoryName] 
FROM 
    [WGCategories]"></asp:SqlDataSource>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# 
    Bind("CategoryName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Product Status Name"
                SortExpression="ProductStatusName">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlProductStatusName"   
runat="server"
                        AppendDataBoundItems="True"
                        DataSourceID="dsProductStatus"
                        DataTextField="ProductStatusName"
                        DataValueField="ProductStatusId"
                        SelectedValue='<%# Bind("ProdStatusId") %>'>
                        <asp:ListItem Value="">[--Make a Selection--]
    </asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvProductStatusName" 
    runat="server"
                        ControlToValidate="ddlProductStatusName"
                        ErrorMessage="*Required"
                        Font-Italic="True"
                        ForeColor="Red"></asp:RequiredFieldValidator>
                    <asp:SqlDataSource ID="dsProductStatus" runat="server"
                        ConnectionString="<%$ ConnectionStrings:WGConString 
%>"
                        SelectCommand="SELECT [ProductStatusId], 
    [ProductStatusName] FROM [WGProductStatuses]"></asp:SqlDataSource>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="ddlProductStatusName" 
runat="server"
                        AppendDataBoundItems="True"
                        DataSourceID="dsProductStatus"
                        DataTextField="ProductStatusName"
                        DataValueField="ProductStatusId"
                        SelectedValue='<%# Bind("ProdStatusId") %>'>
                        <asp:ListItem Value="">[--Make a Selection--]
    </asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsProductStatus" runat="server"
                        ConnectionString="<%$ ConnectionStrings:WGConString 
%>"
                        SelectCommand="SELECT [ProductStatusId], 
    [ProductStatusName] FROM [WGProductStatuses]"></asp:SqlDataSource>
                    <asp:RequiredFieldValidator ID="rfvProductStatusName" 
    runat="server"
                        ControlToValidate="ddlProductStatusName"
                        ErrorMessage="*Required"
                        Font-Italic="True"
                        ForeColor="Red"></asp:RequiredFieldValidator>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# 
    Bind("ProductStatusName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Vendor Name" 
    SortExpression="VendorName">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlVendors" runat="server"
                        DataSourceID="dsVendors"
                        DataTextField="VendorName"
                        DataValueField="VendorId" AppendDataBoundItems="True" 
    SelectedValue='<%# Bind("ProdVendorId") %>'>
                        <asp:ListItem
                            Value="">[--Make a Selection--]</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvVendorName" 
runat="server"
                        ControlToValidate="ddlVendors"
                        ErrorMessage="*Required"
                        Font-Italic="True"
                        ForeColor="Red"></asp:RequiredFieldValidator>
                    <asp:SqlDataSource ID="dsVendors" runat="server"
                        ConnectionString="<%$ ConnectionStrings:WGConString 
%>"
                        SelectCommand="SELECT [VendorId], [VendorName] FROM 
    [WGVendors]"></asp:SqlDataSource>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="ddlVendors" runat="server"
                        DataSourceID="dsVendors"
                        DataTextField="VendorName"
                        DataValueField="VendorId" AppendDataBoundItems="True" 
    SelectedValue='<%# Bind("ProdVendorId") %>'>
                        <asp:ListItem Value="">[--Make a Selection--]
    </asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvVendorName" 
runat="server"
                        ControlToValidate="ddlVendors"
                        ErrorMessage="*Required"
                        Font-Italic="True"
                        ForeColor="Red"></asp:RequiredFieldValidator>
                    <asp:SqlDataSource ID="dsVendors" runat="server"
                        ConnectionString="<%$ ConnectionStrings:WGConString  
%>"
                        SelectCommand="SELECT [VendorId], [VendorName] FROM 
    [WGVendors]"></asp:SqlDataSource>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# 
    Bind("VendorName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" ShowInsertButton="True"   
/>
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="dsSelectedProduct" runat="server"   
ConnectionString="
    <%$ ConnectionStrings:WGConString %>" DeleteCommand="DELETE FROM 
[WGProducts] 
    WHERE [ProdId] = @ProdId" InsertCommand="INSERT INTO [WGProducts]  
    ([ProdName], 
    [Description], [Price], [ModelNumber], [ImageFileName], [LastUpdate], 
    [ProdCategoryId], [ProdStatusId], [ProdVendorId]) VALUES (@ProdName, 
    @Description, @Price, @ModelNumber, @ImageFileName, @LastUpdate,  
    @ProdCategoryId, 
    @ProdStatusId, @ProdVendorId)" SelectCommand="SELECT WGProducts.ProdId, 
    WGProducts.ProdName, WGProducts.Description, WGProducts.Price, 
    WGProducts.ModelNumber, WGProducts.ImageFileName, WGProducts.LastUpdate, 
    WGProducts.ProdCategoryId, WGProducts.ProdStatusId, 
WGProducts.ProdVendorId, 
    WGCategories.CategoryName, WGVendors.VendorName, 
    WGProductStatuses.ProductStatusName FROM WGProducts LEFT OUTER JOIN     
    WGCategories 
    ON WGProducts.ProdCategoryId = WGCategories.CategoryId LEFT OUTER JOIN    
    WGVendors 
    ON WGProducts.ProdVendorId = WGVendors.VendorId LEFT OUTER JOIN 
    WGProductStatuses 
    ON WGProducts.ProdStatusId = WGProductStatuses.ProductStatusId WHERE 
    (WGProducts.ProdId = @ProdId)" UpdateCommand="UPDATE [WGProducts] SET   
    [ProdName] 
    = @ProdName, [Description] = @Description, [Price] = @Price, 
[ModelNumber] = 
    @ModelNumber, [ImageFileName] = @ImageFileName, [LastUpdate] = 
@LastUpdate, 
    [ProdCategoryId] = @ProdCategoryId, [ProdStatusId] = @ProdStatusId, 
    [ProdVendorId] = @ProdVendorId WHERE [ProdId] = @ProdId" 
    OnInserted="dsSelectedProduct_Inserted" 
    OnInserting="dsSelectedProduct_Inserting" 
    OnUpdated="dsSelectedProduct_Updated"    
    OnUpdating="dsSelectedProduct_Updating">
        <DeleteParameters>
            <asp:Parameter Name="ProdId" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ProdName" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="ModelNumber" Type="String" />
            <asp:Parameter Name="ImageFileName" Type="String" />
            <asp:Parameter Name="LastUpdate" Type="DateTime" />
            <asp:Parameter Name="ProdCategoryId" Type="Int32" />
            <asp:Parameter Name="ProdStatusId" Type="Int32" />
            <asp:Parameter Name="ProdVendorId" Type="Int32" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="gvProducts" Name="ProdId" 
    PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ProdName" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="ModelNumber" Type="String" />
            <asp:Parameter Name="ImageFileName" Type="String" />
            <asp:Parameter Name="LastUpdate" Type="DateTime" />
            <asp:Parameter Name="ProdCategoryId" Type="Int32" />
            <asp:Parameter Name="ProdStatusId" Type="Int32" />
            <asp:Parameter Name="ProdVendorId" Type="Int32" />
            <asp:Parameter Name="ProdId" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:LinkButton ID="btnBack" runat="server" OnClick="btnBack_Click">Go 
    Back</asp:LinkButton>
        </asp:View>
    </asp:MultiView>
    <asp:CheckBox ID="cbShowDiscontinued" Text="Show discontinued products" 
    runat="server" AutoPostBack="True" Checked="True" 
    OnCheckedChanged="cbShowDiscontinued_CheckedChanged" />


    </asp:Content>
    <%--<asp:Content ID="Content3" ContentPlaceHolderID="cphSidebar"     
    Runat="Server">
    </asp:Content>--%>

**Below is the Code Behind:**

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    public partial class WGProductsCRUD : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnSoftDelete_PreRender(object sender, EventArgs e)
    {
        #region ToDo: non-working prerender code to fix for dynamic button 
text
        //Button btn=(Button)gvProducts.FindControl("btnSoftDelete");
        //btn.Text = "foo";
        #endregion
    }
    protected void dsSelectedProduct_Inserted(object sender,                  
    SqlDataSourceStatusEventArgs e)
    {
        //re databind gridview
        gvProducts.DataBind();
        //switch views
        mvProducts.SetActiveView(vwMaster);//or mvProducts.ActiveViewIndex=0;
    }
    protected void dsSelectedProduct_Inserting(object sender,                          
    SqlDataSourceCommandEventArgs e)
    {
        //handle file upload logic for an insert (new record)
        FileUpload fupload =
    (FileUpload)dvSelectedProduct.FindControl("fupProdImage");
        //locate the file that is needed inside the details view
        string imageName = "NoImage.jpg";
        //if an image is provided, handle it and send filename to db
        if (fupload.HasFile)
        {
            //get the image name from the fup
            imageName = fupload.FileName;
            //randomly generate the image file name to avoid overwriting 
files 
    with common file names from different users.  Assign randomly generated 
    (GUID) unique names with correct extension (.jpg, .bmp, etc.)
            //get extension of orig file
            string ext = imageName.Substring(imageName.LastIndexOf("."));
            //this will find the . and grab everything after that- the 
extension 
    of the file.
            //Guid it up!
            imageName = Guid.NewGuid() + ext;
            //save the file to the server
            fupload.SaveAs(Server.MapPath("~/Images/Products/" + imageName));
        }
        //after the if statement is done, save the file name string to the 
    database record in the correct column
        e.Command.Parameters["@ImageFileName"].Value = imageName;
    }
    protected void dsSelectedProduct_Updated(object sender, 
    SqlDataSourceStatusEventArgs e)
    {
        //re databind grid view
        gvProducts.DataBind();
        //switch views
        mvProducts.SetActiveView(vwMaster);
    }
    protected void dsSelectedProduct_Updating(object sender, 
    SqlDataSourceCommandEventArgs e)
    {
        //handle file upload logic for updating an existing record
        FileUpload fupload = 
    (FileUpload)dvSelectedProduct.FindControl("fupProdImage");
        //if an image is provided, handle it and send filename to db
        if (fupload.HasFile)
        {
            //get the image name from the fup
            string imageName = fupload.FileName;
            //randomly generate the image file name to avoid overwriting 
files 
    with common file names from different users.  Assign randomly generated 
    (GUID) unique names with correct extension (.jpg, .bmp, etc.)
            //get extension of orig file
            string ext = imageName.Substring(imageName.LastIndexOf("."));
            //this will find the . and grab everything after that- the 
extension 
    of the file.
            //Guid it up!
            imageName = Guid.NewGuid() + ext;
            //save the file to the server
            fupload.SaveAs(Server.MapPath("~/Images/Products/" + imageName));
            //save the file name string to the database record in the correct 
    column
            e.Command.Parameters["@ImageFileName"].Value = imageName;
        }
        //hidden field is two-way bound and will send the existing filename 
back 
    with record being updated
    }
    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        //switch views
        mvProducts.SetActiveView(vwDetails);
        //set details view in insert mode
        dvSelectedProduct.ChangeMode(DetailsViewMode.Insert);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        //switch views 
        mvProducts.SetActiveView(vwMaster);
        //redatabind the gridview
        gvProducts.DataBind();
        //just to be safe : set details view to read only mode
        dvSelectedProduct.ChangeMode(DetailsViewMode.ReadOnly);
    }
    protected void cbShowDiscontinued_CheckedChanged(object sender, EventArgs 
e)
    {
        if (cbShowDiscontinued.Checked)
        {
            gvProducts.DataSourceID = dsDiscontinuedProducts.ID;
        }
        else
        {
            gvProducts.DataSourceID = dsActiveProducts.ID;
        }
        gvProducts.DataBind();//go get the data of either discontinued or 
active 
    products
    }
    }

数据绑定期间集合修改错误

删除 .ID

if (cbShowDiscontinued.Checked)
        {
            gvProducts.DataSourceID = dsDiscontinuedProducts;
        }
        else
        {
            gvProducts.DataSourceID = dsActiveProducts;
        }