Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi my name is vishal for 2 days i have been breaking my head on how to enable managers only to add new users to application in c# windows forms with sql server 2008.
So i am in process of developing my application named:Mini Project in c# windows forms with sql server 2008.
So i login into my application(Mini Project) using default user name:admin and default password:password in through my login form(frmLogin)
Given below is my c# code of how i add a manager(frmManager) using default user name:admin and default password:password.
C#
namespace Mini_Project
{
    public partial class frmManager : Form
    {
        public frmManager()
        {
            InitializeComponent();
        }
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            int autoGenId = -1;
            cmd = new SqlCommand("Insert into ManagerDetail(manager_first_name,manager_last_name,manager_dob,manager_sex,username,password,email,status,row_upd_date)" + "Values(@manager_first_name,@manager_last_name,@manager_dob,@manager_sex,@username,@password,@email,@status,GetDate()); Select @autoGenId=SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@manager_first_name", txtFName.Text);
            cmd.Parameters.AddWithValue("@manager_last_name", txtLName.Text);
            cmd.Parameters.AddWithValue("@manager_dob", dtDOB.Value);
            if (cboSex.SelectedIndex == 0)
            {
                cmd.Parameters.AddWithValue("@manager_sex", "Male");
            }
            else if (cboSex.SelectedIndex == 1)
            {
                cmd.Parameters.AddWithValue("@manager_sex", "Female");
            }
            else if (cboSex.SelectedIndex == 2)
            {
                cmd.Parameters.AddWithValue("@manager_sex", "Transgender");
            }
            cmd.Parameters.AddWithValue("@username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@email", txtEmailID.Text);
            cmd.Parameters.AddWithValue("@status", 1);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            cmd = new SqlCommand("Insert into [dbo].[UserDetail](user_first_name,user_last_name,user_dob,user_sex,user_name,password,email,user_type,stat,row_upd_date)" + "Values(@user_first_name,@user_last_name,@user_dob,@user_sex,@user_name,@password,@email,@user_type,@stat,GetDate())", conn);
            cmd.Parameters.AddWithValue("@user_first_name", txtFName.Text);
            cmd.Parameters.AddWithValue("@user_last_name", txtLName.Text);
            cmd.Parameters.AddWithValue("@user_dob", dtDOB.Value);
            if (cboSex.SelectedIndex == 0)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Male");
            }
            else if (cboSex.SelectedIndex == 1)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Female");
            }
            else if (cboSex.SelectedIndex == 2)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Transgender");
            }
            cmd.Parameters.AddWithValue("@user_name", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@email", txtEmailID.Text);
            cmd.Parameters.AddWithValue("@user_type", 1);
            cmd.Parameters.AddWithValue("@stat", 1);
            cmd.ExecuteNonQuery();
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 2, txtFName.Text.ToString() + "Manager detail was added successfully");
            MessageBox.Show("Manager Detail was added successfully", "Task", MessageBoxButtons.OK, MessageBoxIcon.Information);
            conn.Close();
            this.Close();
}

The above code works Fine!
Given below is my c# code of how i add a new user either with default user name:admin and default password:password and also with username and password from ManagerDetail which i passed the values into UserDetail in above c# code.
C#
namespace Mini_Project
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
        }
private void btnCreate_Click(object sender, EventArgs e)
        {
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            int autoGenId = -1;
            cmd = new SqlCommand("Insert into [dbo].[UserDetail](user_first_name,user_last_name,user_dob,user_sex,user_name,password,email,user_type,stat,row_upd_date,Created_by)" + "Values(@user_first_name,@user_last_name,@user_dob,@user_sex,@user_name,@password,@email,@user_type,@stat,GetDate(),@Created_by); Select @autoGenId=SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@user_first_name", txtFName.Text);
            cmd.Parameters.AddWithValue("@user_last_name", txtLName.Text);
            cmd.Parameters.AddWithValue("@user_dob", dtDOB.Value);
            if (cboSex.SelectedIndex == 0)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Male");
            }
            else if (cboSex.SelectedIndex == 1)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Female");
            }
            else if (cboSex.SelectedIndex == 2)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Transgender");
            }
            cmd.Parameters.AddWithValue("@user_name", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@email", txtEmailID.Text);
            cmd.Parameters.AddWithValue("@user_type", 0);
            cmd.Parameters.AddWithValue("@stat", 1);
            cmd.Parameters.AddWithValue("@Created_by", 1);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 1, txtFName.Text.ToString() + "User detail was added successfully");
            MessageBox.Show("User Detail was added successfully", "Task", MessageBoxButtons.OK, MessageBoxIcon.Information);
            conn.Close();
            this.Close();
}

The above code works Fine.
As you can see the difference between a user and manager is by user_type values in UserDetail(0 for user and 1 for manager).
So i have a mdi parent form named:MDIParent1 which has a MenuStrip control named:menustrip.
In menustrip i have menu named:Admin which has sub-menus which has 2 tool strip menu items.
1st toolstrip name:addManagerToolStripMenuItem,enabled:true,text:Add Manager and visible:true
2nd toolstrip name:addUserToolStripMenuItem,enabled:true,text:Add User and visible:true.
Given below is my c# code of toolstrips:
C#
private void addManagerToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmManager m = new frmManager();
            m.MdiParent = this;
            m.Show();
        }
private void addUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmUser u = new frmUser();
            u.MdiParent = this;
            u.Show();
        }

What i want is when i enter into the application using username and password of user of user_type 0 then i want addManagerToolStripMenuItem and addUserToolStripMenuItem should be disabled when a normal user enters into application.

Suppose when i enter into the application using username and password of user of user_type 1 then i want only addUserToolStripMenuItem should be enabled for that user!

That is what i want. Can anyone help me please! Can anyone help me/guide me on modifications that i need to do in my c# code and where?! Any help/guidance in solving of this problem would be greatly appreciated!
Posted
Comments
ZurdoDev 21-Jul-14 9:56am    
Where exactly are you stuck? All you have to do is write the logic for what you want.
j snooze 21-Jul-14 17:04pm    
Agree with RyanDev. You have just told us the user_type is 0 manager strip should be enabled, if its 1 then user tool strip should be enabled. You have the pseudo code and you have code that accesses the database. All you have to do is check the user type on login and display/hide the appropriate menu.
Member 10248768 23-Jul-14 8:06am    
You are correct i will write pseudo code for that and will ask you sir.
Member 10248768 22-Jul-14 0:02am    
Dear RyanDev and j snooze
the value of user type of normal user:0 and value user type of manager:1 in my table:UserDetail in sql server 2008.
Given below is my c# code of frmLogin(login form) which i have created:

using System.Data.SqlClient;
namespace Mini_Project
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
{
MDIParent1 h = new MDIParent1();
h.Show();
this.Close();
}
else
{
string username = txtUsername.Text;
string password = txtPassword.Text;
bool validUser = ValidateUser(username, password);
if (validUser)
{
MDIParent1 m = new MDIParent1();
m.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid user name or password. Please try with another user name or password", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUsername.Focus();
}
}
}
private bool ValidateUser(string username, string password)
{
bool success = false;
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd = new SqlCommand("Select @count = Count(*) from [dbo].[UserDetail] where username=@username and password=@password", conn);
cmd.Parameters.AddWithValue("@username", txtUsername.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
cmd.Parameters.Add("@count", SqlDbType.Int).Direction = ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
if (Convert.ToInt32(cmd.Parameters["@count"].Value) > 0)
{
success = true;
}
else
{
success = false;
}
conn.Close();
return success;
}
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}

The above code works OK! but not the way i want.
What i want is when a normal user of user_type:0 logs into the application then i want addManagerToolStripMenuItem and addUserToolStripMenuItem should be disabled for that user when he enters into the application.

When an manager user of user_type:1 logins into the application then i want addUserToolStripMenuItem only should be enabled for that user when he enters into the application.
So tell me what modifications of c# code must i do and where to achieve my required result?! Reply please! I am waiting for your reply! I hope i get a reply !

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