Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an grid view. In which Template fields with checkboxes are there for add,view, Edit. If i check the textboxes then in database y value should be inserted otherwise N. my grid view is like this

Role_Id Role_Name Add View Edit Delete
1 Admin chk1 chk2 chk3 chk4
2 User chk1 chk2 chk3 chk4
Posted
Comments
Amalraj Ramesh 7-Mar-14 7:17am    
If you check checkbox immidiatly wants to insert DB???
Member 10578683 7-Mar-14 8:36am    
yes

1 solution

From your example I understood that you have 4 possible rights associated with user roles. So my suggestion is to have into a database a single "int" fields named "Rights" and to store each rights by using "bits fields" stored together in the same int value.
So in place of having 5 (but can be up to 32 or 64 depends on "int type") you could use only one integer field!

Some code example:
XML
/// <summary>
        /// Sets/Clear the specified right into the given user rights flag.
        /// </summary>
        /// <param name="rightsFlag">The user rights flag.</param>
        /// <param name="right">The right.</param>
        /// <param name="value">For true set the right, and for false clear the right.</param>
        /// <returns>The modified user rights flag.</returns>
        private static int SetRightsFlagRight(int rightsFlag, ShopRights right, bool value)
        {
            int flagValue = (int)Math.Pow(2.0, (int)right);
            return (value ? (rightsFlag | flagValue) : (rightsFlag & ~flagValue));
        }

        /// <summary>
        /// Checks the specified right for the given user rights flag.
        /// </summary>
        /// <param name="rightsFlag">The user rights flag.</param>
        /// <param name="right">The right.</param>
        /// <returns>True if the user has the specified right.</returns>
        public static bool CheckRight(int rightsFlag, ShopRights right)
        {
            int flagValue = (int)Math.Pow(2.0, (int)right);
            return (rightsFlag & flagValue) == flagValue;
        }


And you should use an enum for bites fields like the next one:

C#
public enum ShopRights
    {
        View = 0,
        Edit = 1,
        Add = 2,
        Delete = 3,
        // ...
    }
 
Share this answer
 
v3
Comments
Member 10578683 7-Mar-14 7:18am    
can you give some code
Raul Iloc 7-Mar-14 7:24am    
I just update my solution with some code.

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