Hi my name is vishal i was wondering on how to make my global variable accept values from another table in c# windows forms with sql server 2008? So i am developing an application named:Mini Project which has a login form named:frmLogin. Given below is c# code of frmLogin:

namespace Mini_Project
{
    public partial class frmLogin : Form
    {
    public frmLogin()
        {
            InitializeComponent();
        }
        private void frmLogin_Load(object sender, EventArgs e)
        {
        }
        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");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select * from [dbo].[ManagerDetail2] where username='"+txtUsername.Text+"' and password='"+txtPassword.Text+"' and LoginAttempts< 3", conn);
            SqlDataReader rd = cmd.ExecuteReader();
           while(rd.Read())
            {
                success=true;
                Module.MUser_ID = Convert.ToInt32(rd[0].ToString());
                Module.MUserName = rd[1].ToString();
            }
            rd.Close();
            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");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select * from [dbo].[UserDetail2] where username='"+txtUsername.Text+"' and password='"+txtPassword.Text+"' and LoginAttempts< 3", conn);
            SqlDataReader rd = cmd.ExecuteReader();
           while(rd.Read())
            {
                success=true;
                Module.User_ID = Convert.ToInt32(rd[0].ToString());
                Module.UserName = rd[1].ToString();
            }
            rd.Close();
            conn.Close();
            return success;
        }
        private void btnLogin_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;
            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)
                {
                    cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                    Module.User_ID = 1;
                    MDIParent1 m = new MDIParent1();
                    m.Show();
                    this.Close();
                }
                     if (validmanager)
                    {
                        cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                        cmd.ExecuteNonQuery();
                        Module.MUser_ID = 1;
                        MDIParent1 g = new MDIParent1();
                        g.Show();
                        this.Close();
                    }    
                    else
                    {
                        cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                    cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                        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 to some extent!
So using default username:admin and default password:password i enter/login into application as admin and add manager to help assign tasks to users. Given below is my c# code of form(frmManager) which i add datas into table:ManagerDetail2 in sql server 2008:

namespace Mini_Project
{
    public partial class frmManager : Form
    {
    public int bGenId = -1;
        public frmManager()
        {
            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;
            cmd = new SqlCommand("Insert into [dbo].[ManagerDetail2](manager_first_name,manager_last_name,manager_dob,manager_sex,email,username,password,status,created_by,LoginAttempts,row_upd_date)" + "Values(@manager_first_name,@manager_last_name,@manager_dob,@manager_sex,@email,@username,@password,@status,@created_by,@LoginAttempts,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("@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();
            bGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
             cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
            cmd.ExecuteNonQuery();
            ((MDIParent1)this.MdiParent).updateUserActivities(bGenId, 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 structure of table:ManagerDetail2 in sql server2008:

ColumnName                               DataType                  AllowNulls
manager_id(auto-increment primary key)     Int                        No
manager_first_name                      nvarchar(50)                  Yes
manager_last_name                       nvarchar(50)                  Yes
manager_dob                                date                       Yes
manager_sex                             nvarchar(20)                  Yes
email                                   nvarchar(60)                  Yes
username                                nvarchar(25)                  Yes
password                                nvarchar(15)                  Yes
status                                      bit                       Yes
created_by                                  Int                       Yes
LoginAttempts                               Int                       Yes
row_upd_date                             datetime                     Yes

Using username and password from table:ManagerDetail2 i enter/login into application as manager,as a manager i create new users(normal users) who will come under me.Given below is my c# code of form(frmUser) which as a manager adds new users to application and into table:UserDetail2 in sql server2008:

namespace Mini_Project
{
    public partial class frmUser : Form
    {
    public int autoGenId = -1;
        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;
            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",Module.Manager);
            cmd.Parameters.AddWithValue("@LoginAttempts", 0);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
            cmd.ExecuteNonQuery();
            ((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 only to some extent only!
Given below is structure of my table:UserDetail2 in sql server 2008:

ColumnName                            DataType                    AllowNulls
user_id(auto-increment primary key)     Int                         No
user_first_name                       nvarchar(50)                  Yes
user_last_name                        nvarchar(50)                  Yes
user_dob                                date                        Yes
user_sex                              nvarchar(50)                  Yes
email                                 nvarchar(60)                  Yes
username                              nvarchar(25)                  Yes
password                              nvarchar(15)                  Yes
status                                  bit                         Yes
LoginAttempts                           Int                         Yes
created_by                              Int                         Yes
row_upd_date                           datetime                     Yes

Given below is c# code of my class named:Module

namespace Mini_Project
{
    class Module
    {
        public static int AUser_ID;
        public static int Admin
        {
            get { return AUser_ID; }
            set { AUser_ID = value; }
        }
        public static int User_ID;
        public static int User
        {
            get { return User_ID; }
            set { User_ID = value; }
        }
        public static int MUser_ID;
        public static int Manager
        {
            get { return MUser_ID; }
            set { MUser_ID = value; }
        } 
        public static string UserName="";
        public static string GlobalUser
        {
            get { return UserName; }
            set { UserName = value; }
        }
        public static string MUserName="";
        public static string GlobalManager
        {
            get { return MUserName; }
            set { MUserName = value; }
        }
        public static string AUserName="";
        public static string GlobalAdmin
        {
            get { return AUserName; }
            set { AUserName = value; }
        }
    }
}

The problem i am facing is:So i enter as admin using default username and password,add new managers for tasks.Then i enter enter as manager using username and password from table:ManagerDetail2,add new users to application and under me. So after creating new user to application using username and password from ManagerDetail2 in login form(frmLogin) when i see field :created_by in table:UserDetail2 i get only 1 as value for all created users.
What i want is as each manager having username and password has different manager_id. So when i enter into application as manager(using username and password from table:ManagerDetail2),create new user to application and under me then i want manager_id value of currently enter manager to be passed into field:created_by in table:UserDetail2 that is what i want!

Can anyone help me please! Can anyone tell me/guide me what modifications must i need to do in my c# code and where? to achieve my required result?! Can anyone help me please! to help me achieve my required output?! Any help/guidance in solving of this problem would be greatly appreciated!

Dear,
I suggest that, You should make class "Module" a singleton, something like:

namespace Mini_Project
{
    public sealed class Module
    {

        static readonly Module _instance = new Module();
        public static readonly Module Instance
        {get{return _instance;}}

        public static int AUser_ID;
        public static int Admin
        {
            get { return AUser_ID; }
            set { AUser_ID = value; }
        }
        public static int User_ID;
        public static int User
        {
            get { return User_ID; }
            set { User_ID = value; }
        }
        public static int MUser_ID;
        public static int Manager
        {
            get { return MUser_ID; }
            set { MUser_ID = value; }
        } 
        public static string UserName="";
        public static string GlobalUser
        {
            get { return UserName; }
            set { UserName = value; }
        }
        public static string MUserName="";
        public static string GlobalManager
        {
            get { return MUserName; }
            set { MUserName = value; }
        }
        public static string AUserName="";
        public static string GlobalAdmin
        {
            get { return AUserName; }
            set { AUserName = value; }
        }
    }
}

Assigned them once

Module.Instance.Admin = 1;

You can later call for assigned value to anyother class/s with out instantiating the calss again an again.

well, take a look at Singleton Design pattern, It's useful :)

Dear xeesh
Thank you for replying to my post/question on such short notice. However i have some problem/trouble implementing the solution which you generously gave/helped me.I have made modifications to my class:Module as you have told me. Given below is c# code of my class named:Module

namespace Mini_Project
{
    public sealed class Module
    {
        static readonly Module _instance = new Module();
        public static readonly Module Instance
        { get { return _instance; } }
        public static int AUser_ID;
        public static int Admin
        {
            get { return AUser_ID; }
            set { AUser_ID = value; }
        }
        public static int User_ID;
        public static int User
        {
            get { return User_ID; }
            set { User_ID = value; }
        }
        public static int MUser_ID;
        public static int Manager
        {
            get { return MUser_ID; }
            set { MUser_ID = value; }
        }
        public static string UserName = "";
        public static string GlobalUser
        {
            get { return UserName; }
            set { UserName = value; }
        }
        public static string MUserName = "";
        public static string GlobalManager
        {
            get { return MUserName; }
            set { MUserName = value; }
        }
        public static string AUserName = "";
        public static string GlobalAdmin
        {
            get { return AUserName; }
            set { AUserName = value; }
        }
    }
}

The problem i am facing is in my login form named:frmLogin.
Given below is c# code of frmLogin where i am facing problem:

namespace Mini_Project
{
    public partial class frmLogin : Form
    {
 public frmLogin()
        {
            InitializeComponent();
        }
        private void frmLogin_Load(object sender, EventArgs e)
        {

        }
         private void btnLogin_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;
            if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
            {
                Module.Instance.Admin = 1;
                MDIParent1 h = new MDIParent1();
                h.Show();
                this.Close();
            }

In above code i am facing error(compilation error) telling:"Error:Member 'Mini Project.Module.Admin.get' cannot be accessed with instance reference;qualify it with a type name instead"

Tell me/guide me on how to solve my problem and what modifications must i need to do in my c# code and where?
Reply please! I am waiting for your reply! I hope i get reply from you Sir!

Dear vishal,
Sorry, I didn't check the code before posting.
Anyway you can simplify your Module class further more. You don't need to make static every property of class

namespace Mini_Project
{

public sealed class Module
    {
        private static readonly Module _instance = new Module();

        public static Module Instance
        {
            get { return _instance; }
        }

        public int Admin { get; set; }
        public int User { get; set; }
        public  int Manager { get; set; }
        public string GlobalUser { get; set; }
        public string GlobalManager { get; set; }
        public string GlobalAdmin { get; set; }
    }
}

Way of using it e.g.

Module.Instance.Admin = 1;
MessageBox.Show(Module.Instance.Admin.ToString());

Dear xeesh
Thank you for replying to my post/question on such short notice. I have made changes to my class named:Module as you have told me/guided me.Given below is my c# code of my class named:Module

namespace Mini_Project
{
    public sealed class Module
    {
        private static readonly Module _instance = new Module();

        public static Module Instance
        {
            get { return _instance; }
        }
        public int Admin { get; set; }
        public int User { get; set; }
        public int Manager { get; set; }
        public string GlobalUser { get; set; }
        public string GlobalManager { get; set; }
        public string GlobalAdmin { get; set; }
    }
}

In above code i get no compile errors or runtime errors.! Given below is c# code of my login form named:frmLogin

namespace Mini_Project
{
    public partial class frmLogin : Form
    {
    public frmLogin()
        {
            InitializeComponent();
        }
        private void frmLogin_Load(object sender, EventArgs e)
        {

        }
        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");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select manager_id,manager_first_name from [dbo].[ManagerDetail2] where username='"+txtUsername.Text+"' and password='"+txtPassword.Text+"' and LoginAttempts< 3", conn);
            SqlDataReader rd = cmd.ExecuteReader();
           while(rd.Read())
            {
                success=true;
                Module.Instance.Manager = Convert.ToInt32(rd[0].ToString());
                Module.Instance.GlobalManager = rd[1].ToString();
            }
            rd.Close();
            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");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select * from [dbo].[UserDetail2] where username='"+txtUsername.Text+"' and password='"+txtPassword.Text+"' and LoginAttempts< 3", conn);
            SqlDataReader rd = cmd.ExecuteReader();
           while(rd.Read())
            {
                success=true;
                Module.Instance.User = Convert.ToInt32(rd[0].ToString());
                Module.Instance.GlobalUser = rd[1].ToString();
            }
            rd.Close();
            conn.Close();
            return success;
        }
         private void btnLogin_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;
            if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
            {
                Module.Instance.Admin = 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)
                {
                    cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                    Module.Instance.User = 1;
                    MDIParent1 m = new MDIParent1();
                    m.Show();
                    this.Close();
                }
                     if (validmanager)
                    {
                        cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                        cmd.ExecuteNonQuery();
                        Module.Instance.Manager = 1;
                        MDIParent1 g = new MDIParent1();
                        g.Show();
                        this.Close();
                    }    
                    else
                    {
                        cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                    cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                        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();
        }

So using default username:admin and default password:password,i enter/login into application(Mini Project) as a admin, i create manager for tasks.Given below is c# code of form(frmManager) from which i insert values into table:ManagerDetail2 in sql server 2008 through c# windows forms.

namespace Mini_Project
{
    public partial class frmManager : Form
    {
    public int bGenId = -1;
        public frmManager()
        {
            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;
            cmd = new SqlCommand("Insert into [dbo].[ManagerDetail2](manager_first_name,manager_last_name,manager_dob,manager_sex,email,username,password,status,created_by,LoginAttempts,row_upd_date)" + "Values(@manager_first_name,@manager_last_name,@manager_dob,@manager_sex,@email,@username,@password,@status,@created_by,@LoginAttempts,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("@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();
            bGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            cmd = new SqlCommand("Update [dbo].[ManagerDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
            cmd.ExecuteNonQuery();
            ((MDIParent1)this.MdiParent).updateUserActivities(bGenId, 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();
        }

So using username **and **password from table:ManagerDetail2 i enter/login into application(Mini Project) as a manager,i create new users to application,users who will come under me only! Each manager has different manager_id as field:manager_id in table:ManagerDetail2 is auto-increment primary key which is of data type:Int.
Given below is structure of table:ManagerDetail2 in sql server 2008:

ColumnName                          DataType         AllowNulls
manager(auto-increment primary key)   Int             No
manager_first_name                   nvarchar(50)     Yes
manager_last_name                    nvarchar(50)     Yes
manager_dob                            date           Yes
manager_sex                          nvarchar(20)     Yes
username                             nvarchar(25)     Yes
password                             nvarchar(15)     Yes
email                                nvarchar(60)     Yes
status                                 bit            Yes
created_by                             Int            Yes
LoginAttempts                          Int            Yes
row_upd_date                           datetime       Yes

Using username and password from table:ManagerDetail2,manager_id(eachg manager having different manager_id) i create/add new users to application,users who will come under me only! Given below is c# code of form(frmUser) through which i insert values into table:UserDetail2

namespace Mini_Project
{
    public partial class frmUser : Form
    {
     public int autoGenId = -1;
        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;
            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",Module.Instance.Manager);
            cmd.Parameters.AddWithValue("@LoginAttempts", 0);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
             cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
            cmd.ExecuteNonQuery();
            ((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 problem i am still facing in above code ie: i login/enter into application as a manager using username and password from table:ManagerDetail2 who also has different manager_id,create/add new user to application.
Upon executing the above c# code of form(frmUser) i get value 1 in my field named:created_by in my table:UserDetail2 for all users even though each user has been created by different managers.
Tell me how to solve this problem?! Tell me/help me/guide me on what modifications must i need to do in my c# code and where? Reply please Sir?! I am waiting for your reply! I hope i get reply from you Sir!

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.