Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
The below code is working fine but i am looking to add more feature to this code as i need when i delete the user via below code as well delete all records into database belong to the same UID in others tables: ads, CVs, FavComp, FavJob, FavoCv, favourite, JobApplic, jobs, Orders, ShoppingCart.

C#
protected void DeltUsrbtn_Command(object sender, CommandEventArgs e)
        {
            using (SqlConnection DeltUsrSQLCon = new SqlConnection(sc))
            {
                System.Data.SqlClient.SqlCommand DeltUsrcmd = new System.Data.SqlClient.SqlCommand();
                DeltUsrcmd.CommandType = System.Data.CommandType.Text;

                DeltUsrcmd.CommandText = "DELETE FROM UserInfo WHERE UID = @UID";

                DeltUsrcmd.Parameters.AddWithValue("@UID", e.CommandArgument.ToString());
                DeltUsrcmd.Connection = DeltUsrSQLCon;
                DeltUsrSQLCon.Open();

                DeltUsrcmd.ExecuteNonQuery();
                DeltUsrSQLCon.Close();
                ReBindDeltUsrsGV();
                admusrdeletedlbl.Text = "User has been deleted successfully";
            }
        }
Posted
Comments
PIEBALDconsult 23-Oct-15 23:02pm    
Never delete. Have an attribute that indicates that it is inactive.
Santosh K. Tripathi 24-Oct-15 13:51pm    
you are right. in app never delete data just mark it isDeleted = true.

and for more complex logic use stored procedure. :)
jgakenhe 24-Oct-15 0:37am    
The you'll need to change your query to delete in multiple tables.
DELETE FROM UserInfo WHERE UID = @UID
GO
DELETE FROM Table2 WHERE UID = @UID.

It would be much more elegant if all of it was in 1 stored procedure.
Arkadeep De 24-Oct-15 1:53am    
Either write delete command for all tables or write a trigger which will fire after delete a row from your primary table. And use any flag like IsDelete (bit). and update the row data with making the value of IsDelete true/false.

For trigger visit this one -- http://stackoverflow.com/questions/9996643/sql-server-on-delete-trigger?answertab=active#tab-top
Santosh K. Tripathi 25-Oct-15 3:52am    
you are correct.

1 solution

There are many ways to do this, but since we are talking about this function then the solution would be, (as Arkadeep said in his comment to your question), to add the delete commands for other tables too. Which would delete the data from other tables also, once it has finished deleting from one table.

C#
SqlCommand command = new SqlCommand("DELETE FROM ads WHERE UID = @UID", connection);
SqlCommand command = new SqlCommand("DELETE FROM CVs WHERE UID = @UID", connection);
SqlCommand command = new SqlCommand("DELETE FROM FavComp WHERE UID = @UID", connection);
SqlCommand command = new SqlCommand("DELETE FROM FavJob WHERE UID = @UID", connection);
...


This way, these commands would delete the record for the user with that ID in other tables also.

Another way would be to implement referential integrity[^] and store the UserInfo as Master table. So that the child would hold the data for those records (only!) which exist in the master table. If master deletes or updates on record, other tables update themselves based on the new data. This method would take a bit of time as you may need to learn about the concept first.
 
Share this answer
 
Comments
Santosh K. Tripathi 24-Oct-15 13:54pm    
what will happen if after 1st commend, sql connection Fail?
Afzaal Ahmad Zeeshan 24-Oct-15 14:00pm    
It would stop! What else? :-)

You need to make sure that there won't be any problem in the process.
Santosh K. Tripathi 25-Oct-15 3:47am    
:) you are correct. so my suggestion is "Member 10690878" should move whole logic in database. i mean he should use Stored Procedure.
Afzaal Ahmad Zeeshan 25-Oct-15 3:56am    
Stored Procedures can also work.
Santosh K. Tripathi 25-Oct-15 3:48am    
one more thing making connecting to database several time is not good idea. it is very expensive.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900