+1  A: 

SQL 2005 will enforce locks. Before a row can be updated the transaction must acquire an exclusive lock on it. Only 1 transaction can be granted this at a time so the other one will have to wait for that transaction to commit (2 phase locking) before being granted the lock that it needs for the update.

The second write will "win" in that it will overwrite the first one. You can implement optimistic concurrency controls in the sqldatasource to detect that the row has changed and abort the second one rather than blindly overwriting the first edit.

Edit

Following clarification to the question. If you want to roll your own you could add a timestamp column to the table (In SQL Server 2005 this is updated automatically when a row is updated) and bring that back as a hidden dataitem in the gridview then in your UPDATE statement add a where clause UPDATE ... WHERE PrimaryKeyColumn=@PKValue AND TimeStampCol=@OriginalTimestampValue If no rows were affected (retrievable from ExecuteNonQuery - generally) then another transaction modified the row. This might be a bit more lightweight than the alternative used by the data source control where it passes back the original values of all columns and adds them into the WHERE clause with similar logic.

Martin Smith
thanks, optimistic.... have seen this option in the advance...for sqldatasource control but the problem here is i am not using one. please have a look at the edit and help out thankyou. how can i make this "check that data changed or not before update"
@user287745 Maybe I misunderstood your first sentence then? I thought you were talking about the sqldatasource control?
Martin Smith
no not using this control, am displaying results in gridview my sqlreader and than asking user to enter new values in textboxs, then execute update statement, crude way but have to learn sql statements no controls regarding it :-(. so i guess what i am asking is is there a way to detect a change in database row of that particular table before the update query gets executed
+2  A: 

This is solved by most applications using Optimistic Concurency control. Applications simply add more conditions to the update WHERE clause in order to detect changes that occured between the time the data was read and the moment the update is applied. Is called optimistic concurency because the applicaiton assumes no concurent changes will occur, and if they do occur they are are detected and the appplicaiton has to restart the operation. The alternative to optimistic concurency is pesimistic concurency where the applications explicitly locks the data it plans to update. In practice operaitons involving user interaction are never done under pesimistic concurency model.

Other concurency model, specially in distributed applications, is the one implied by the Fiefdom and Emissaries model.

So while database locks and transaction concurency models are always omnipresent in any database operation, when user interaction is involved no application will ever rely on the database locks. User interactions are simply way to long in terms of database transactions. Acquiring locks for the while forgetful Fred is out to lunch and has a data screen open on his desktop simply doesn't work.

Remus Rusanu