Click here to Skip to main content
15,885,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi my name is vishal for past 2 days i have been breaking my head on how to have list users only created by that manager when the same manager enters into the form in c# windows forms with sql server 2008.

So i have also created a form for registering normal user named:frmUser and given below is my c# code of that form:
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].[UserDetail2](user_first_name,user_last_name,user_dob,user_sex,email,username,password,status,row_upd_date,created_by,LoginAttempts)" + "Values(@user_first_name,@user_last_name,@user_dob,@user_sex,@email,@username,@password,@status,GetDate(),@created_by,@LoginAttempts); 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("@email", txtEmailID.Text);
            cmd.Parameters.AddWithValue("@username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@status", 1);
            cmd.Parameters.AddWithValue("@Created_by", 1);
            cmd.Parameters.AddWithValue("@LoginAttempts", 0);
            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 + "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!
i have login form(frmLogin) given below is c# code of frmLogin:
C#
namespace Mini_Project
{
    public partial class frmLogin : Form
    {
 public frmLogin()
        {
            InitializeComponent();
        }
 private bool ManagerUser(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].[ManagerDetail2] where username=@username and password=@password and LoginAttempts< 3", 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;
                cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                cmd.ExecuteNonQuery();
            }
            else
            {
                success = false;
                cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
            return success;
        }
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].[UserDetail2] where username=@username and password=@password and LoginAttempts< 3", 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;
                cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                cmd.ExecuteNonQuery();
            }
            else
            {
                success = false;
                cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
            return success;
        }
private void btnLogin_Click(object sender, EventArgs e)
        {
            if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
            {
                Module.AUser_ID=1;
                MDIParent1 h = new MDIParent1();
                h.Show();
                this.Close();
            }
            else
            {
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                bool validUser = ValidateUser(username, password);
                bool validmanager = ManagerUser(username, password);
                    if (validUser)
                    {
                        Module.User_ID = 1;
                        MDIParent1 m = new MDIParent1();
                        m.Show();
                        this.Close();
                    }
                     if (validmanager)
                    {
                        Module.MUser_ID = 1;
                        MDIParent1 g = new MDIParent1();
                        g.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Invalid user name or password. Please try tomorow ", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtUsername.Focus();
                    }
                }
            }
 private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

The above code works Fine!
I have form named:frmTask which enabled manager to assign a task to a user which he has created/registered into the application.
Given below is my c# code of frmTask:
C#
private void frmTask_Load(object sender, EventArgs e)
        {
            dtAssignDate.MinDate = dtAssignDate.Value;
            string user=("Select u.user_id as user_id,(u.user_first_name+' '+u.user_last_name+'|'+right('000'+convert(varchar,u.user_id),5)) as Name from UserDetail2 u where u.status=1");
            DataTable mt = new DataTable();
            SqlConnection cont = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (cont.State != ConnectionState.Open)
            {
                cont.Open();
            }
            SqlCommand hcr = new SqlCommand(user, cont);
            mt.Load(hcr.ExecuteReader());
            lstUsers.DataSource = mt;
            lstUsers.ValueMember = "user_id";
            lstUsers.DisplayMember = "Name";
            lstUsers.SelectedValue = 0;
        }

The above code works to some extent and not the way i want!

The problem i am facing is i am getting all users from table:UserDetail2 into listbox(lstUsers).

What i want is when a manager(has username and password) enters into application,registers/add new users to application. When that same manager has entered into that application and when he enters into frmTask i want only list of users created by him only in a listbox?

Can anyone help me please!
Can anyone tell me what modifications must i need to do in my c# code and where? Can anyone help me/guide me to solve my problem?! Any help/guidance in solving of this problem would be greatly appreciated!
Posted
Updated 27-Jul-14 21:21pm
v2

1 solution

Do the following changes in frmTask

C#
private void frmTask_Load(object sender, EventArgs e)
        {
            dtAssignDate.MinDate = dtAssignDate.Value;
            string user=("Select u.user_id as user_id,(u.user_first_name+' '+u.user_last_name+'|'+right('000'+convert(varchar,u.user_id),5)) as Name from UserDetail2 u where u.status=1 and created_by"+Module.MUser_ID.ToString());
            DataTable mt = new DataTable();
            SqlConnection cont = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (cont.State != ConnectionState.Open)
            {
                cont.Open();
            }
            SqlCommand hcr = new SqlCommand(user, cont);
            mt.Load(hcr.ExecuteReader());
            lstUsers.DataSource = mt;
            lstUsers.ValueMember = "user_id";
            lstUsers.DisplayMember = "Name";
            lstUsers.SelectedValue = 0;
        }


I have added only "created_by" filter to the existing query.

If this doesn't resolve your problem, please let me know :)
 
Share this answer
 

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