用dataSource.DeleteCommand删除数据时,在什么时间里面捕捉错误的反馈信息
谢紫枫 2006-07-10 01:25:49 比如我用dataSource1作为gridview1的数据源,采用存储过程进行删除,如下
dataSource1.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
dataSource1.DeleteCommand = "Stored_Procedures1";
在存储过程中有错误反馈信息,如下
CREATE procedure dbo.Stored_Procedures1
(@id int)
as
if exists (select * table1
where sign='Y'AND id = @id )
begin
raiserror('已操作的编号不可以删除!',16,1)
return
end
else
begin
delete from table1 where id = @id
end
GO
这样如果sign='N'则顺利执行删除,可当sign='Y'是,就会跳到错误界面了
我用自定义的button1,删除text1里面的编号时,利用如下代码进行的错误捕捉
protected void del_coupon_Click(object sender, EventArgs e)
{
SqlCommand add_coupon_code = new SqlCommand();
add_coupon_code.Connection = conn;
add_coupon_code.CommandType = CommandType.StoredProcedure;
add_coupon_code.CommandText = "Stored_Procedures1";
add_coupon_code.Parameters.Add("id", int).Value = id.Text;
try
{
conn.Open();
add_coupon_code.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script lanuage=javascript>alert('已删除')</script>");
}
catch (System.Exception Ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script lanuage=javascript>alert('" + Ex.Message + "')</script>");
}
finally
{
conn.Dispose();
}
}
请问类似的情况在用dataSource.DeleteCommand删除时,捕捉事件应该写在什么事件里面?
我用RowDeleted,RowDeleting或者是在dataSource1.DeleteCommand建立try,catch都没有效果
或是我的写法有问题,请指教,谢谢!