删除按钮/功能删除SQL上的错误数据

时间:2013-06-28 11:20:38

标签: c# sql-server-2008 visual-studio-2012

我正在使用SQL Server Management Studio备份的Visual Studio 2012作为其数据库。我正在使用gridview来查看我的数据。我使用了TemplateField,我可以编辑和删除数据。但似乎当我删除某个数据时,它总是删除第一个数据。可能是什么解决方案?

这是我的asp代码:

<asp:Content ID="Content4" ContentPlaceHolderID="full_main" Runat="Server">
    <asp:GridView ID="gvUsers" runat="server" 
    class="table table-bordered data-table" AutoGenerateColumns="False" 
    AllowPaging="True" onpageindexchanging="gvUsers_PageIndexChanging" 
        onselectedindexchanged="gvUsers_SelectedIndexChanged" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="UserID" HeaderText="User ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="UserID" />
            <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                SortExpression="LastName" />
            <asp:BoundField DataField="EmailAddress" HeaderText="Email Adress" SortExpression="EmailAddress" />
            <asp:BoundField DataField="Street" HeaderText="Street" SortExpression="Street" />
            <asp:BoundField DataField="Municipality" HeaderText="Municipality" SortExpression="Municipality" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" SortExpression="ZipCode" />
            <asp:BoundField DataField="ContactNo" HeaderText="Contact No" SortExpression="ContactNo" />
            <asp:TemplateField HeaderText="Actions">
                <ItemTemplate>
                    <a href="http://localhost:12345/CAPSTONE/Users/Edit.aspx?ID=<%# Eval("UserID") %>" 
                                class="btn btn-primary btn-mini">Edit</a>
                            <a href="#deleteItem" data-toggle="modal" class="btn btn-danger btn-mini">Delete</a>
                            <div id="deleteItem" class="modal hide">
                              <div class="modal-header">
                                <button data-dismiss="modal" class="close" type="button">×</button>
                                <h3>Record Deletion</h3>
                              </div>
                              <div class="modal-body">
                                <p>Are you sure you want to delete the record?</p>
                              </div>
                              <div class="modal-footer"> 
                                <a class="btn btn-primary"
                                    href="http://localhost:12345/CAPSTONE/Users/Delete.aspx?ID=<%# Eval("UserID") %>">Confirm</a> 
                                <a data-dismiss="modal" class="btn" href="#">Cancel</a> 
                              </div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            <center>
                <h1>
                    No records found.</h1>
            </center>
        </EmptyDataTemplate>
        <PagerStyle CssClass="pagination alternate" />
    </asp:GridView>

这是我背后的代码:

void DeleteUser(int userID)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "DELETE FROM Users WHERE UserID=@UserID";
        cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = userID;
        cmd.ExecuteNonQuery();
        con.Close();
        Helper.AddLog(Session["userid"].ToString(), "Delete", "Deleted a User");
        Response.Redirect("Default.aspx");
    }

0 个答案:

没有答案