Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using delete sql query to delete role of a particular user form the database.. but the problem is that.. if user has more than one role. than its erasing all the role of a particular user from the database. can i delete row on selection bases?
what approach i should use to do that.. ??




protected void _rowdeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbldeletename = (Label)row.FindControl("lblusername");
        
        MAconn.Open();
        SqlCommand cmd = new SqlCommand("DELETE FROM Roles WHERE UserId = (SELECT u.UserId FROM Users u WHERE u.UserName = @UserName", MAconn);
        cmd.Parameters.AddWithValue("@UserName", lbldeletename.Text);
        cmd.ExecuteNonQuery();
        MAconn.Close();
        bind();
 
    }
Posted

Deleting roles from a user requires a REVOKE command, this link would provide more details on this:

http://beginner-sql-tutorial.com/sql-grant-revoke-privileges-roles.htm[^]
 
Share this answer
 
You Should pass role id in to query.....

SQL
SqlCommand cmd = new SqlCommand("DELETE FROM Roles WHERE RoleId =@RoleId AND UserId = (SELECT u.UserId FROM Users u WHERE u.UserName = @UserName", MAconn);
 
Share this answer
 
Comments
codegeekalpha 11-Sep-11 17:03pm    
i have a textbox for wherer UserName= @UserName..

what i have to do for @RoleId . i don't want to specify the @roleid Textbox
jatinchandarana 12-Sep-11 13:44pm    
You can provide check box list for each role and delete selected role...
or you can provide button near user id text box when user click on that button then you can display associate role with them and delete selected role.......

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