【发布时间】:2016-09-10 14:03:42
【问题描述】:
我正在尝试更新 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))
);
去 创建非聚集索引 [CategoriesProducts] ON [dbo].[Products]([CategoryID] ASC);
去 创建非聚集索引 [CategoryID] ON [dbo].[Products]([CategoryID] ASC);
去 创建非聚集索引 [ProductName] ON [dbo].[Products]([ProductName] ASC);
去 创建非聚集索引 [SupplierID] ON [dbo].[Products]([SupplierID] ASC);
去 创建非聚集索引 [供应商产品] ON [dbo].[Products]([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的列吗? -
是的,北风数据库里有这么一列
-
您是否发布了
products表的架构? -
异常的具体细节是什么?
-
我只是添加了我得到的异常的图像。