当前位置: 代码迷 >> ASP.NET >> GridView绑定SqlDataSource后不能删除?解决方案
  详细解决方案

GridView绑定SqlDataSource后不能删除?解决方案

热度:1623   发布时间:2013-02-26 00:00:00.0
GridView绑定SqlDataSource后不能删除?
<div>  
<asp:GridView   ID= "GridView1 "  
DataSourceID= "SqlDataSource1 "  
AutoGenerateDeleteButton= "true "  
AutoGenerateEditButton= "true "  
runat= "server ">  
</asp:GridView>  

<asp:SqlDataSource   ID= "SqlDataSource1 "  
runat= "server "  
ProviderName= "System.Data.SqlClient "  
ConnectionString= "Data   Source=GOLDKING;Initial   Catalog=Northwind;User   ID=sa;Password=iq50628 "  
UpdateCommand= "Update   Customers   SET   CompanyName=@CompanyName,Address=@Address,City=@City,PostalCode=@PostalCode,Country=@Country   where   CustomerID   =   @CustomerID "  
DeleteCommand= "Delete   from   Customers   where   CustomerID   =   @CustomerID "  
SelectCommand= "SELECT   [CustomerID],   [CompanyName],   [Address],   [City],   [PostalCode],   [Country]   FROM   [Customers] ">  
<UpdateParameters>  
<asp:Parameter   Name= "CustomerID "   />  
<asp:Parameter   Name= "CompanyName "   />  
<asp:Parameter   Name= "Address "   />  
<asp:Parameter   Name= "City "   />  
<asp:Parameter   Name= "PostalCode "   />  
<asp:Parameter   Name= "Country "   />  
</UpdateParameters>  
</asp:SqlDataSource>  
</div>  
---  
报错:必须声明变量   '@CustomerID '。   请问该如何解决,出现问题的原因是什么?

------解决方案--------------------------------------------------------
删除的情况,你必须指定gridview的DataKeyNames来唯一标示每一行,你这里应该是DataKeyNames= "CustomerID "
------解决方案--------------------------------------------------------
DataKeyNames的详细说明,自己查MSDN
  相关解决方案