ProductName'在选定的数据源上找不到

本文关键字:数据源 找不到 ProductName | 更新日期: 2023-09-27 18:06:24

我试图在gridview中更新一行,我得到这样的错误:在选定的数据源上找不到名称为"ProductName"的字段或属性。

我正在使用北风数据库。

这是我的代码:

protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandText = "UPDATE Products SET ProductName = @ProductName WHERE ProductID =@ ProductID";
    command.Parameters.AddWithValue("@ProductID", Convert.ToInt32(gvProducts.DataKeys[e.RowIndex]["ProductID"]));
    TextBox tb = (TextBox)gvProducts.Rows[e.RowIndex].Cells[0].Controls[0];
    command.Parameters.AddWithValue("@ProductName", tb.Text);
    int effect = 0;
    try
    {
        connection.Open();
        effect = command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    }
    finally
    {
        connection.Close();
        gvProducts.EditIndex = -1;
        gvProducts.DataBind();
    }
    if (effect != 0)
        fillDetailed();
}

数据库方案为:

CREATE TABLE [dbo].[Products] (
[ProductID]       INT           IDENTITY (1, 1) NOT NULL,
[ProductName]     NVARCHAR (40) NOT NULL,
[SupplierID]      INT           NULL,
[CategoryID]      INT           NULL,
[QuantityPerUnit] NVARCHAR (20) NULL,
[UnitPrice]       MONEY         CONSTRAINT [DF_Products_UnitPrice] DEFAULT ((0)) NULL,
[UnitsInStock]    SMALLINT      CONSTRAINT [DF_Products_UnitsInStock] DEFAULT ((0)) NULL,
[UnitsOnOrder]    SMALLINT      CONSTRAINT [DF_Products_UnitsOnOrder] DEFAULT ((0)) NULL,
[ReorderLevel]    SMALLINT      CONSTRAINT [DF_Products_ReorderLevel] DEFAULT ((0)) NULL,
[Discontinued]    BIT           CONSTRAINT [DF_Products_Discontinued] DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ([ProductID] ASC),
CONSTRAINT [FK_Products_Categories] FOREIGN KEY ([CategoryID]) REFERENCES [dbo].[Categories] ([CategoryID]),
CONSTRAINT [FK_Products_Suppliers] FOREIGN KEY ([SupplierID]) REFERENCES [dbo].[Suppliers] ([SupplierID]),
CONSTRAINT [CK_Products_UnitPrice] CHECK ([UnitPrice]>=(0)),
CONSTRAINT [CK_ReorderLevel] CHECK ([ReorderLevel]>=(0)),
CONSTRAINT [CK_UnitsInStock] CHECK ([UnitsInStock]>=(0)),
CONSTRAINT [CK_UnitsOnOrder] CHECK ([UnitsOnOrder]>=(0))

);

去创建非聚集索引[类别产品]在[dbo]。[产品]([被]ASC);

去创建非聚集索引[CategoryID]在[dbo]。[产品]([被]ASC);

去创建非聚集索引[ProductName]在[dbo]。[产品]([ProductName] ASC);

去创建非聚集索引[SupplierID]在[dbo]。[产品]([SupplierID] ASC);

去创建非聚集索引[SuppliersProducts]在[dbo]。[产品]([SupplierID] ASC);

异常

这是。aspx:

<asp:GridView ID="gvProducts" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="gvProducts_RowCancelingEdit" OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="ProductName" HeaderText="Производ" />
            <asp:BoundField DataField="UnitPrice" HeaderText="Единечна цена" />
            <asp:BoundField DataField="Quantity" HeaderText="Количина" />
            <asp:CommandField CancelText="Откажи" EditText="Уреди" ShowEditButton="True" UpdateText="Промени" />
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

ProductName'在选定的数据源上找不到

您使用的是哪种数据源?对象数据源/Sql数据源?请检查在创建数据源后是否更改了表定义,并且未刷新数据源。在这种情况下,表定义中更新的列将不会被反映,并且可能发生这样的异常。