2013-03-03 56 views
2

我有這個GridViewMySql的GridView的刪除命令

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" 
     GridLines="None"> 
     <AlternatingRowStyle BackColor="White" /> 
     <Columns> 
      <asp:TemplateField ShowHeader="False"> 
        <EditItemTemplate> 
         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
          CommandName="Update" Text="Update"></asp:LinkButton> 
         &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
          CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
          CommandName="Edit" Text="Edit"></asp:LinkButton> 
         &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
          CommandName="Delete" 
          onclientclick="javascript:return confirm('are you sure you want to delete.');" 
          Text="Delete"></asp:LinkButton> 
        </ItemTemplate> 
       </asp:TemplateField> 
      <asp:BoundField DataField="id" HeaderText="id" SortExpression="id"/> 
      <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" /> 
      <asp:BoundField DataField="first" HeaderText="first" SortExpression="first" /> 
      <asp:BoundField DataField="last" HeaderText="last" SortExpression="last" /> 
      <asp:BoundField DataField="gender" HeaderText="gender" 
       SortExpression="gender" /> 
      <asp:BoundField DataField="birthday" HeaderText="birthday" 
       SortExpression="birthday" /> 
     </Columns> 
     <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
     <SortedAscendingCellStyle BackColor="#FDF5AC" /> 
     <SortedAscendingHeaderStyle BackColor="#4D0000" /> 
     <SortedDescendingCellStyle BackColor="#FCF6C0" /> 
     <SortedDescendingHeaderStyle BackColor="#820000" /> 
    </asp:GridView> 

這是SqlDataSource

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:clientConnectionString %>" 
     ProviderName="<%$ ConnectionStrings:clientConnectionString.ProviderName %>" 
     SelectCommand="SELECT * FROM Client;" 
     DeleteCommand="DELETE FROM Client WHERE id = @id AND email = @email;"> 

     <DeleteParameters> 
      <asp:Parameter Name="id" Type="String" /> 
      <asp:Parameter Name="email" Type="String" /> 
     </DeleteParameters> 

    </asp:SqlDataSource> 

,當我嘗試到EXCUTE刪除我得到這個錯誤:

Parameter '@id' must be defined. 
Exception Details: MySql.Data.MySqlClient.MySqlException: Parameter '@id' must be defined. 

回答

2

您可能需要將DataKeyNames屬性添加到您的GridView

必須設置DataKeyNames屬性才能使GridView控件的自動更新和刪除功能正常工作。這些關鍵字段的值傳遞給數據源控件,以便指定要更新或刪除的行。看到這個link

DataKeyNames="id,email" 

在Gridview的上下文中。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" 
     GridLines="None" DataKeyNames="id,email">