[英]If Sql Server returns an error, then SqlException should be thrown. But it isn't
If I bind GridView to SqlDataSource and also set AutoGenerateEditButton to true, and if I then try to update a field ( this field being a primary key in database ), then database should return an error and thus SqlException should be thrown?! 如果我将GridView绑定到SqlDataSource并将AutoGenerateEditButton也设置为true,然后尝试更新一个字段(该字段是数据库中的主键),那么数据库应该返回错误,因此应该抛出SqlException? So why doesn't page report an exception? 那么,为什么页面不报告异常? Instead, all Gridview does is setting all fields in that row back to their original values. 相反,所有Gridview所做的就是将该行中的所有字段都设置回其原始值。
thanx 谢谢
EDIT: 编辑:
Hello, 你好,
When I executed same update statement with the following code, I got Cannot update identity column 'EmployeeID' exception , so I'm assuming Sql Server did report same error when GridView tried to update, but for some reason exception wasn't raised: 当我使用以下代码执行相同的更新语句时,出现无法更新标识列'EmployeeID'的异常 ,因此我假设GridView尝试更新时Sql Server确实报告了相同的错误,但由于某种原因未引发异常:
SqlConnection sc = new SqlConnection();
sc.ConnectionString = @"Data source=localhost; integrated security=sspi; initial catalog=northwind;";
SqlCommand sComand = new SqlCommand();
sComand.Connection = sc;
sComand.CommandText = "update Employees set EmployeeId=100,FirstName='Suzy',"+
"LastName='Smile',City='Moon' where EmployeeId=1";
sc.Open();
int I = sComand.ExecuteNonQuery();
SECOND EDIT: 第二编辑:
Is your DataKeyNames property set correctly? 您的DataKeyNames属性设置正确吗?
I tried with setting DataKeyNames="EmployeeId", but exception was still not raised 我尝试设置DataKeyNames =“ EmployeeId”,但仍未引发异常
If your SQL query returns a result, then C# will get that result. 如果您的SQL查询返回结果,则C#将获得该结果。 If your SQL query throws an exception, then C# will get that exception. 如果您的SQL查询引发异常,则C#将获得该异常。 You cannot mix and match here, it's not a wardrobe. 您不能在这里混搭,这不是衣柜。
An exception will be thrown ONLY if the ERROR severity in T-SQL is 16 or higher AFAIK. 仅当T-SQL中的错误严重性为16或更高AFAIK时,才会引发异常。 So not all errors are the same in SQL Server. 因此,并非所有错误在SQL Server中都是相同的。
实际上,您应该能够无误地修改主键,只要它不会导致重复即可。
您的DataKeyNames属性设置正确吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.