Is it possible to update two tables using SQL Data Source and ASP.NET Grid View? I have the following SQL Query for the Select statement.
SELECT
tbl_user
<asp:SqlDataSource runat="server":
SelectCommand="SELECT ... "
UpdateCommand="UPDATE a; UPDATE b;"
DeleteCommand="DELETE FROM a; DELETE FROM b;" />
Doesn't it work?
CREATE PROC UpdateUser
(
@ID int
,@Pass varchar(15)
,@Email varchar(75)
) AS
BEGIN
Update
tbl_user_login
Set
Pass = @Pass
Where
ID = @ID
Update
tbl_user_profile
Set
Email1 = @Email
Where
ID = @ID
END
You can write multiple sql commands into a single sproc as I have above. Expand updates as needed. Deletes work the same way...
Yes, it's possible. Here is how to do it in a safe way:
CREATE PROCEDURE UpdateUser (@ID int, @Pass varchar(15), @Email varchar(75)) AS
BEGIN TRANSACTION
UPDATE tbl_user_login SET Pass = @Pass WHERE ID = @ID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
UPDATE tbl_user_profile SET Email1 = @Email WHERE ID = @ID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
COMMIT
In this way you don't need to be worried in case there is some error raised by any of the updates. Which means: If any of them will fail, the whole operation will be canceled and you will not have inconsistent data in your DB.
More about the use of Transactions
here: http://www.4guysfromrolla.com/webtech/080305-1.shtml
The basic DELETE statement is: DELETE FROM <tablename> WHERE <condition>