views:

211

answers:

4

Hello,


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?! So why doesn’t page report an exception? Instead, all Gridview does is setting all fields in that row back to their original values.


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:

        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?

I tried with setting DataKeyNames="EmployeeId", but exception was still not raised

+2  A: 

If your SQL query returns a result, then C# will get that result. If your SQL query throws an exception, then C# will get that exception. You cannot mix and match here, it's not a wardrobe.

Samuel
+2  A: 

An exception will be thrown ONLY if the ERROR severity in T-SQL is 16 or higher AFAIK. So not all errors are the same in SQL Server.

Andrei Rinea
+2  A: 

You should actually be able to modify the primary key without error, so long as it doesn't result in a duplicate.

Dave Cluderay
+1  A: 

Is your DataKeyNames property set correctly?

JP Alioto