在所选数据源上找不到名为“ProductName”的字段或属性

时间:2016-09-10 14:03:43

标签: c# asp.net

我正在尝试更新gridview中的一行,我收到如下错误: 在所选数据源上找不到名为“ProductName”的字段或属性。

我正在使用Northwind数据库。

这是我的代码:

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

);

GO 创建非集群指数[CategoriesProducts]     ON [dbo]。[产品]([CategoryID] ASC);

GO 创建非集群索引[CategoryID]     ON [dbo]。[产品]([CategoryID] ASC);

GO 创建非集群索引[产品名称]     ON [dbo]。[产品]([ProductName] ASC);

GO 创建非集群指数[SupplierID]     ON [dbo]。[产品]([SupplierID] ASC);

GO 创建非集群指数[供应商产品]     ON [dbo]。[产品]([SupplierID] ASC);

exception

这是.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>

1 个答案:

答案 0 :(得分:0)

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