编辑/删除表格选项

时间:2011-11-30 19:05:15

标签: c# asp.net sqldatasource

我有一个表格,每行都有编辑和删除链接。我应该可以单击编辑或删除,它会这样做。编辑链接有效但删除时出现此错误:

除非指定了DeleteCommand,否则数据源'SqlDataSource1'不支持删除。

我的表数据源是sqlDataSource1。

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"           
        SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]">
    </asp:SqlDataSource>

</div>
    <div align="center">
    <asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<p>
    <asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label>
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
    <asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</p>
        <p>
            <asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label>
            <asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server" 
                onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged">
                <asp:ListItem>A</asp:ListItem>
                <asp:ListItem>U</asp:ListItem>
            </asp:DropDownList>
</p>
        <p>
            <asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click1" 
    Text="Add User" /> 


</p>
        <p>
            <asp:Label ID="lblError" runat="server"></asp:Label>
</p>

    </div>
<p>
    &nbsp;</p>
            <div align="center">
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False" 
    DataSourceID="AccessDataSource1">
    <Columns>
        <asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False" 
            SortExpression="UserID"></asp:BoundField>
        <asp:BoundField DataField="UserName" HeaderText="UserName" 
            SortExpression="UserName"></asp:BoundField>
        <asp:BoundField DataField="UserPassword" HeaderText="UserPassword" 
            SortExpression="UserPassword"></asp:BoundField>
        <asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel" 
            SortExpression="SecurityLevel"></asp:BoundField>
        <asp:CommandField ShowEditButton="True"></asp:CommandField>
        <asp:CommandField ShowDeleteButton="True"></asp:CommandField>




    </Columns>
</asp:GridView>
                </form>
</body>

1 个答案:

答案 0 :(得分:1)

该表没有主键,这就是您无法自动生成更新和删除语句的原因。

可能是这样的:

<asp:SqlDataSource
    id="AccessDataSource1"
    runat="server"
    DataSourceMode="DataSet"
    ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"  
    SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM tblUserLogin"
    DeleteCommand="DELETE FROM [tblUserLogin] WHERE UserID=@UserID">
 <DeleteParameters>
    <asp:Parameter Name="UserID" Type="Int32" />
 </DeleteParameters>

</asp:SqlDataSource>

您必须向SqlDataSource添加DeleteCommand。当然,您必须更改ConnectionString和tableNames。在DataGridView中,您有一个CommandField来显示DeleteButton。这就是你的SqlDataSource需要DeleteCommand的原因。

如果您想通过向导执行此操作,可以通过单击右上角的小箭头来配置SqlDataSource。在高级选项下,您可以生成插入,更新和删除命令,因为您可能也需要它们。

有一个很好的tutorial about SqlDataSource