Can anyone perhaps help me with my problem please?

I want to apply security to my winform application but I am stuck..

This is the code I have that will run when the view job button is clicked and I it doesn't work

private void viewJobBtn_Click(object sender, EventArgs e)
        {
            //Getting the sql connection and selecting columns in database
            SqlConnection conn = new SqlConnection("Data Source=10.0.3.45;Initial Catalog=JobbookDatabase;User ID=sa;Password=123");

            conn.Open();
            SqlCommand cmdDelete = new SqlCommand("SELECT UserType FROM UserAccounts where Username = '" + displayCurrentUserLbl.Text + "'", conn);
            cmdDelete.ExecuteNonQuery();
            conn.Close();

            //the temporary variables used
            string type = "3";

                try
                {
                    //if type is right, form will continue
                    if (userAccessLevelLbl.Text.Equals(type))
                    {
                        try
                        {
                            //Reading the selected row in the current datagrid and then passing it on to the viewing form
                            String JobID = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[0].Value.ToString();
                            String Month = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[1].Value.ToString();
                            String Date = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[2].Value.ToString();
                            String Office = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
                            String JobCode = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
                            bool Pending = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value;
                            String AllocatedTo = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
                            String JobDescription = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[7].Value.ToString();
                            String InvoiceNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[8].Value.ToString();
                            String Client = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[9].Value.ToString();
                            String ContactDetails = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[10].Value.ToString();
                            String Amount = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[11].Value.ToString();
                            String Assistants = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[12].Value.ToString();
                            bool Surveyd = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[13].Value;
                            bool PlanCompleted = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[14].Value;
                            bool PlanGivenToClient = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[15].Value;
                            bool LetterOfApproval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[16].Value;
                            bool Invoiced = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[17].Value;
                            bool Paid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[18].Value;
                            String TypeOfJob = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[19].Value.ToString();
                            bool FoundationStakeout = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[20].Value;
                            bool BrickWork = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[21].Value;
                            bool Application = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[22].Value;
                            bool Approval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[23].Value;
                            bool Appeal = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[24].Value;
                            bool Endorsements = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[25].Value;
                            bool SGfeesPaid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[26].Value;
                            bool SubmitToSG = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[27].Value;
                            bool PlansReceived = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[28].Value;
                            ViewJob vj = new ViewJob(this, JobID, Month, Date, Office, JobCode, Pending, AllocatedTo, JobDescription, InvoiceNumber, Client, ContactDetails, Amount, Assistants, Surveyd, PlanCompleted, PlanGivenToClient, LetterOfApproval, Invoiced, Paid, TypeOfJob, FoundationStakeout, BrickWork, Application, Approval, Appeal, Endorsements, SGfeesPaid, SubmitToSG, PlansReceived);
                            vj.Show();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Select a job first");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Access Denied");
                    }
                }
                catch (Exception re)
                {
                    MessageBox.Show("Access Denied" + re);
                }
        }

Recommended Answers

All 17 Replies

Can anyone perhaps help me with my problem please?

I want to apply security to my winform application but I am stuck..

This is the code I have that will run when the view job button is clicked and I it doesn't work

private void viewJobBtn_Click(object sender, EventArgs e)
        {
            //Getting the sql connection and selecting columns in database
            SqlConnection conn = new SqlConnection("Data Source=10.0.3.45;Initial Catalog=JobbookDatabase;User ID=sa;Password=123");

            conn.Open();
            SqlCommand cmdDelete = new SqlCommand("SELECT UserType FROM UserAccounts where Username = '" + displayCurrentUserLbl.Text + "'", conn);
            cmdDelete.ExecuteNonQuery();
            conn.Close();

            //the temporary variables used
            string type = "3";

                try
                {
                    //if type is right, form will continue
                    if (userAccessLevelLbl.Text.Equals(type))
                    {
                        try
                        {
                            //Reading the selected row in the current datagrid and then passing it on to the viewing form
                            String JobID = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[0].Value.ToString();
                            String Month = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[1].Value.ToString();
                            String Date = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[2].Value.ToString();
                            String Office = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
                            String JobCode = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
                            bool Pending = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value;
                            String AllocatedTo = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
                            String JobDescription = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[7].Value.ToString();
                            String InvoiceNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[8].Value.ToString();
                            String Client = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[9].Value.ToString();
                            String ContactDetails = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[10].Value.ToString();
                            String Amount = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[11].Value.ToString();
                            String Assistants = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[12].Value.ToString();
                            bool Surveyd = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[13].Value;
                            bool PlanCompleted = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[14].Value;
                            bool PlanGivenToClient = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[15].Value;
                            bool LetterOfApproval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[16].Value;
                            bool Invoiced = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[17].Value;
                            bool Paid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[18].Value;
                            String TypeOfJob = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[19].Value.ToString();
                            bool FoundationStakeout = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[20].Value;
                            bool BrickWork = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[21].Value;
                            bool Application = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[22].Value;
                            bool Approval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[23].Value;
                            bool Appeal = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[24].Value;
                            bool Endorsements = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[25].Value;
                            bool SGfeesPaid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[26].Value;
                            bool SubmitToSG = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[27].Value;
                            bool PlansReceived = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[28].Value;
                            ViewJob vj = new ViewJob(this, JobID, Month, Date, Office, JobCode, Pending, AllocatedTo, JobDescription, InvoiceNumber, Client, ContactDetails, Amount, Assistants, Surveyd, PlanCompleted, PlanGivenToClient, LetterOfApproval, Invoiced, Paid, TypeOfJob, FoundationStakeout, BrickWork, Application, Approval, Appeal, Endorsements, SGfeesPaid, SubmitToSG, PlansReceived);
                            vj.Show();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Select a job first");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Access Denied");
                    }
                }
                catch (Exception re)
                {
                    MessageBox.Show("Access Denied" + re);
                }
        }

Can you please tell the actual error/problem that needs attention. As you said it didn't work, then we should know what was your expectation and what you get in response.

Can anyone perhaps help me with my problem please?

I want to apply security to my winform application but I am stuck..

This is the code I have that will run when the view job button is clicked and I it doesn't work

private void viewJobBtn_Click(object sender, EventArgs e)
        {
            //Getting the sql connection and selecting columns in database
            SqlConnection conn = new SqlConnection("Data Source=10.0.3.45;Initial Catalog=JobbookDatabase;User ID=sa;Password=123");

            conn.Open();
            SqlCommand cmdDelete = new SqlCommand("SELECT UserType FROM UserAccounts where Username = '" + displayCurrentUserLbl.Text + "'", conn);
            cmdDelete.ExecuteNonQuery();
            conn.Close();

            //the temporary variables used
            string type = "3";

                try
                {
                    //if type is right, form will continue
                    if (userAccessLevelLbl.Text.Equals(type))
                    {
                        try
                        {
                            //Reading the selected row in the current datagrid and then passing it on to the viewing form
                            String JobID = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[0].Value.ToString();
                            String Month = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[1].Value.ToString();
                            String Date = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[2].Value.ToString();
                            String Office = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
                            String JobCode = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
                            bool Pending = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value;
                            String AllocatedTo = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
                            String JobDescription = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[7].Value.ToString();
                            String InvoiceNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[8].Value.ToString();
                            String Client = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[9].Value.ToString();
                            String ContactDetails = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[10].Value.ToString();
                            String Amount = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[11].Value.ToString();
                            String Assistants = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[12].Value.ToString();
                            bool Surveyd = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[13].Value;
                            bool PlanCompleted = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[14].Value;
                            bool PlanGivenToClient = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[15].Value;
                            bool LetterOfApproval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[16].Value;
                            bool Invoiced = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[17].Value;
                            bool Paid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[18].Value;
                            String TypeOfJob = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[19].Value.ToString();
                            bool FoundationStakeout = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[20].Value;
                            bool BrickWork = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[21].Value;
                            bool Application = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[22].Value;
                            bool Approval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[23].Value;
                            bool Appeal = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[24].Value;
                            bool Endorsements = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[25].Value;
                            bool SGfeesPaid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[26].Value;
                            bool SubmitToSG = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[27].Value;
                            bool PlansReceived = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[28].Value;
                            ViewJob vj = new ViewJob(this, JobID, Month, Date, Office, JobCode, Pending, AllocatedTo, JobDescription, InvoiceNumber, Client, ContactDetails, Amount, Assistants, Surveyd, PlanCompleted, PlanGivenToClient, LetterOfApproval, Invoiced, Paid, TypeOfJob, FoundationStakeout, BrickWork, Application, Approval, Appeal, Endorsements, SGfeesPaid, SubmitToSG, PlansReceived);
                            vj.Show();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Select a job first");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Access Denied");
                    }
                }
                catch (Exception re)
                {
                    MessageBox.Show("Access Denied" + re);
                }
        }

Instead of writing this long line every time:

dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[0].Value.ToString();

You should manage it like this:

DataGridViewCellCollection dgridRow = DataGridView1.SelectedRows[0].Cells;
..
...
...
...
bool Application = Convert.ToBoolean(dgridRow[22].Value);
bool Approval = Convert.ToBoolean(dgridRow[23].Value);
...
..
..
..

I just need some advice on how to apply permission in my windows application. In my database I have a column called userType. This consists of data ranging from 1 to 5, where 5 is the highest permission. Now I want to apply those rules to my application.

Take for example the view job button above. Only users with a userType 2 and above may be allowed to view a job. If not, a messagebox should pop up and display "Access denied".

Hi!

you can use this code:

enum Roles
        { 
           NotAdmin =1
        }

        public bool IsInRole(string usertype)
        {
            foreach(Roles r in Enum.GetValues(typeof(Roles)))
            {
                if (usertype == r.ToString())
                    return true;
            }
            return false;
        }

"Roles" will contain all the roles you allowed to have access so if "IsInRole" return "true" this means you they are allowed other wise Not Allowed.

Thanks for the reply :)

Seems that this could work, but how do I assign it to my viewjob button when clicked. Stupid question I know :P

Hi!

you need to alter your Line 12 of your original code:

string type = "3";

like this: (complete code)

private void button2_Click(object sender, EventArgs e)
        {
            //Getting the sql connection and selecting columns in database
            SqlConnection conn = new SqlConnection("Data Source=10.0.3.45;Initial Catalog=JobbookDatabase;User ID=sa;Password=123");

            conn.Open();
            SqlCommand cmdDelete = new SqlCommand("SELECT UserType FROM UserAccounts where Username = '" + displayCurrentUserLbl.Text + "'", conn);
            cmdDelete.ExecuteNonQuery();
            conn.Close();

            //the temporary variables used
            //it has numeric value as I see from your original code so little change
            //IsInRole() Method  
            string type = .........;  // here value will come from either DB or 
                                      // user input from textbox etc

            try
            {
                //if type is right, form will continue
                if (IsInRole(type))
                {
                    try
                    {
                        //Reading the selected row in the current datagrid and then passing it on to the viewing form
                        String JobID = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[0].Value.ToString();
                        String Month = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[1].Value.ToString();
                        String Date = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[2].Value.ToString();
                        String Office = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
                        String JobCode = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
                        bool Pending = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value;
                        String AllocatedTo = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
                        String JobDescription = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[7].Value.ToString();
                        String InvoiceNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[8].Value.ToString();
                        String Client = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[9].Value.ToString();
                        String ContactDetails = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[10].Value.ToString();
                        String Amount = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[11].Value.ToString();
                        String Assistants = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[12].Value.ToString();
                        bool Surveyd = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[13].Value;
                        bool PlanCompleted = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[14].Value;
                        bool PlanGivenToClient = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[15].Value;
                        bool LetterOfApproval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[16].Value;
                        bool Invoiced = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[17].Value;
                        bool Paid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[18].Value;
                        String TypeOfJob = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[19].Value.ToString();
                        bool FoundationStakeout = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[20].Value;
                        bool BrickWork = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[21].Value;
                        bool Application = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[22].Value;
                        bool Approval = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[23].Value;
                        bool Appeal = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[24].Value;
                        bool Endorsements = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[25].Value;
                        bool SGfeesPaid = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[26].Value;
                        bool SubmitToSG = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[27].Value;
                        bool PlansReceived = (bool)dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[28].Value;
                        //ViewJob vj = new ViewJob(this, JobID, Month, Date, Office, JobCode, Pending, AllocatedTo, JobDescription, InvoiceNumber, Client, ContactDetails, Amount, Assistants, Surveyd, PlanCompleted, PlanGivenToClient, LetterOfApproval, Invoiced, Paid, TypeOfJob, FoundationStakeout, BrickWork, Application, Approval, Appeal, Endorsements, SGfeesPaid, SubmitToSG, PlansReceived);
                        //vj.Show();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Select a job first");
                    }
                }
                else
                {
                    MessageBox.Show("Access Denied");
                }
            }
            catch (Exception re)
            {
                MessageBox.Show("Access Denied" + re);
            }
        }

        enum Roles
        { 
           NotAdmin = 1
        }

        public bool IsInRole(string usertype)
        {
            foreach(Roles r in Enum.GetValues(typeof(Roles)))
            {
                if ([B]Convert.ToInt32(usertype) == int.Parse(r.ToString())[/B])  // since your UserType has numeric value so compare with "int" value  
                    return true;
            }
            return false;
        }

Almost there :)

One last error I seem to get. It says "Input string was not in a correct format". It seems to be at this line:

if (IsInRole(type))

Any ideas?

What is the value of "type" variable ???? and what are possible values of "type" variable ???

The value I have for type is the following:

string type = userAccessLevelLbl.Text;

userAccessLevlLbl is lbl in this form, hidden, and it gets the accesslevel of the user by passing it to the current form. Nothing complicated, but I'm thinking my problem lies here..?

Hi!

your approach is correct that the point of concern is this part:

string type = userAccessLevelLbl.Text;

I asked you about the possible value that 'userAccessLevelLbl' should have, so we need to take appropriate action on the line I highlighted below:

public bool IsInRole(string usertype)
        {
            foreach(Roles r in Enum.GetValues(typeof(Roles)))
            {
[B]                if (Convert.ToInt32(usertype) == int.Parse(r.ToString()))[/B]  // since your UserType has numeric value so compare with "int" value  
                    return true;
            }
            return false;
        }

I still get that error. :(

What if I perhaps change my column type in the database to lets say text, string? Then we don't have to convert it...?

I didn't answer my question that: What can be the possible value of "userAccessLevelLbl". I can't take appropriate action until and unless not get answer.

The possible value should at least be 2 or above up to 5. If it is 1 access should be denied. Sorry if I can't explain it clear enough.

In my database the column has values from 1 to 5. In this case 2 and above are allowed. The useraccesslbl displays the access level of the current logged in user. That level is fetched from the previous form and passed on to here, displayed in a label, nut not visible because it's only for authentication purposes

Hi!

Your IsInRole Method should be:

public bool IsInRole(string usertype)
        {
            foreach (Roles r in Enum.GetValues(typeof(Roles)))
            {
                if (Convert.ToInt32(usertype) == (int)r)  // since your UserType has numeric value so compare with "int" value  
                    return false;
            }
            return (Convert.ToInt32(usertype) < 6 ? true : false);
        }

Thanks :) I've managed to make your code work. Thanks for the help :)

adata thanks for those helpful links. They made some stuff clear to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.