简体   繁体   English

无法从表中删除最后一行

[英]Cannot Delete Final Row From Table

The situation is a user can add modules to their profile, upon adding a row they are redirected to a page containing a gridview. 情况是用户可以将模块添加到他们的配置文件中,添加一行后,它们将被重定向到包含gridview的页面。 The gridview displays all their currently selected modules. 网格视图显示其所有当前选择的模块。 Each row of the gridview contains a "delete" button which allows the user to delete a module from their profile. gridview的每一行都包含一个“删除”按钮,该按钮允许用户从其概要文件中删除模块。 Users have no problem deleting modules if the gridview row count > 1. However when only one row remains in the table then pressing the "delete" button does not remove the row. 如果gridview行数> 1,则用户删除模块没有问题。但是,当表中仅剩一行时,按“删除”按钮不会删除该行。 The only way to delete the row is to logout and log back in again. 删除行的唯一方法是注销并再次登录。 I do not understand why this happens. 我不明白为什么会这样。 I am sure that it is not a problem with the stored procedure as i have manually executed the sp and have been able to delete rows from the table, no matter how many rows remain. 我肯定这与存储过程无关,因为我已经手动执行了sp,并且无论剩余多少行,都能够从表中删除行。

Here is the code executed upon button press: 这是按下按钮时执行的代码:

else if (e.CommandName == "Remove")
            {
                //deleteUserModule
                Debug.WriteLine("userID: " + userID + ", code: " + moduleCode);
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CMS"].ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("deleteUserModule", conn)
                        {
                            CommandType = CommandType.StoredProcedure
                        })
                    {
                        cmd.Parameters.Add(new SqlParameter("@moduleCode", SqlDbType.NVarChar)).Value = moduleCode;
                        cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.Int)).Value = userID;
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        conn.Dispose();
                    }
                }
                moduleCode = String.Empty;
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            }

Any suggestions as to why the final row can only be deleted after a logout and log back in? 关于为什么最后一行只能在注销并重新登录后才能删除的任何建议?

UPDATE: After checking out the exception thrown, thanks to the advice of @BrendanGreen, i have changed the refresh statement from: 更新:在检查抛出异常后,由于@BrendanGreen的建议,我将刷新语句从以下位置更改:

Page.Response.Redirect(Page.Request.Url.ToString(), true);

To: 至:

Response.Redirect("myModules.aspx", false);
                Context.ApplicationInstance.CompleteRequest();

The problem no longer persists 问题不再存在

After checking out the exception thrown, thanks to the advice of @BrendanGreen, i have changed the refresh statement from: 在检查抛出的异常之后,由于@BrendanGreen的建议,我将刷新语句从:

Page.Response.Redirect(Page.Request.Url.ToString(), true);

To: 至:

Response.Redirect("myModules.aspx", false);
            Context.ApplicationInstance.CompleteRequest();

The problem no longer persists 问题不再存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM