C# 编辑/删除表格选项

C# 编辑/删除表格选项,c#,asp.net,sqldatasource,C#,Asp.net,Sqldatasource,我有一个表,每行都有编辑和删除链接。我应该能够点击编辑或删除,它会这样做。“编辑”链接起作用,但“删除”有以下错误: 数据源“SqlDataSource1”不支持删除,除非指定了DeleteCommand 我的表数据源是sqlDataSource1 <asp:SqlDataSource ID="SqlDataSource1" runat="server" <asp:SqlDataSource ID="SqlDataSource1" runat="server"

我有一个表,每行都有编辑和删除链接。我应该能够点击编辑或删除,它会这样做。“编辑”链接起作用,但“删除”有以下错误:

数据源“SqlDataSource1”不支持删除,除非指定了DeleteCommand

我的表数据源是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>


A. U


该表没有主键,因此无法自动生成更新和删除语句

可能是这样的:

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

简单的谷歌搜索可能会给你答案?你没有指定删除参数。你的用户ID来自哪里?是在控件中指定的数据键。不要担心被否决的选票。早上我会投票给你,因为我已经没有票了。任何人提出问题都不应被否决。您没有指定删除参数。你的用户ID来自哪里?是在控件中指定的数据键。不要担心被否决的选票。早上我会投票给你,因为我已经没有票了。任何人都不应该因为提问而被否决。另外,你使用的是哪种类型的CompositeControl?GriView,DataGrid…?@BrokenGlass:问题是数据库中缺少主键。这就是为什么无法生成更新、插入和删除命令。对这样的问题投否决票是不公平的谢谢,我在上面添加了我的html代码。我不确定要添加什么或在哪里添加。对不起,我想我现在已经全部拥有了。我尝试插入它,但我想我要么放错了位置,要么我需要替换一些东西。我犯了一些错误。我应该把它放在哪里?谢谢,您必须将DeleteCommand放在元素中,就在SelectCommand之后,正如我在代码中所示。错误消息可能是因为尚未声明参数UserID。我将为此添加一些代码,但正如我所说的,您最好自动生成所有内容,因为您可能还需要更新和插入。现在,当我运行它时,我得到一个错误,上面写着:关键字不受支持:“提供者”