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.
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.
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:
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!