简体   繁体   English

ASP.NET 2.0存储过程删除不起作用

[英]ASP.NET 2.0 stored procedure delete not working

I have an event that fires on a button click that calls a stored procedure that deletes the selected row from the database. 我有一个事件在按钮单击时触发,该事件调用从数据库中删除所选行的存储过程。

In debugging this I have already hard coded the stored procedure with an id to make sure the delete was working on SQL side so I wont post the stored procedure. 在调试这个我已经使用id硬编码存储过程以确保删除在SQL端工作,所以我不会发布存储过程。 I also went into my page and on selecting the record to delete made sure that the hidden field was being set to the id of the record. 我也进入了我的页面并选择了要删除的记录,确保隐藏字段被设置为记录的id。

When I click I see the post back occur so I see the button firing, and it may be something small and stupid with my code or a permissions issue I am not aware of or where to look for. 当我点击时,我看到发回的帖子,所以我看到按钮触发,它可能是一些小而愚蠢的我的代码或权限问题,我不知道或在哪里寻找。

The parameter for the stored procedure is an integer, and I am not hitting the catch or throwing errors of any kind the data just is not being deleted. 存储过程的参数是一个整数,我没有遇到捕获或抛出任何类型的错误,数据只是没有被删除。

Thanks for any help on this issue as always it is much appreciated. 感谢您对此问题的任何帮助,我们非常感谢。

This is the code that fires when the button is clicked. 这是单击按钮时触发的代码。

if (isAdmin)
{
    SqlConnection cnx = new SqlConnection(server);
    SqlCommand cmd = new SqlCommand("STOREDPROCNAME", cnx);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@VARIABLEFORPROC", Convert.ToInt16(hiddenfield1.Value.ToString()));

    try
    {
       cnx.Open();
       cmd.ExecuteNonQuery();
       cnx.Close();
    }
    catch (Exception ex)
    {
       throw new Exception("Error executing MyProcedureName.", ex);
    }
}

fillDataGrids();

The SQL Int type maps to the C# Int32 type . SQL Int类型映射到C#Int32类型 It's possible that you're getting data loss during your conversion. 转换过程中可能会丢失数据。 Try replacing 尝试更换

cmd.Parameters.AddWithValue("@VARIABLEFORPROC", Convert.ToInt16(hiddenfield1.Value.ToString()));

with

cmd.Parameters.AddWithValue("@VARIABLEFORPROC", Convert.ToInt32(hiddenfield1.Value.ToString()));

您必须在SQL之后尝试存储过程的准确性,而不是在事务上尝试并在DB上记录异常,您必须确定错误发生在哪里

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

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