Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
    public partial class Admin_Control : Form
    {
        Form1.User aa;
        public string sa;
        private Form1.User ua;

        public Admin_Control(Form1.User s1)
        {
            InitializeComponent();
            this.ua = ua;
            aa = s1;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Main mn = new Main(ua);
            mn.Show();
            this.Hide();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.ColumnIndex == 1)
            {

                OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\IT Inventory management\\IT Inventory Database.accdb");
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandText = "UPDATE Login SET UserRole ='Admin'";
                con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("User Role Updated");
            }
            else
            {
                MessageBox.Show("Changes Failed");
            }
              
        }
        private void show()
        {
            DataTable dt = new DataTable();
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\IT Inventory management\\IT Inventory Database.accdb");
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandText = "SELECT * FROM Login";
            con.Open();
            OleDbDataReader adr= cmd.ExecuteReader();
            dt.Load(adr);
            dataGridView1.DataSource = dt;
            //dataGridView1.DataBind();
         DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
         btn.HeaderText = "Change User Role";
         btn.Name = "btn_change";
         btn.Text = "Change";
         btn.UseColumnTextForButtonValue = true;
         dataGridView1.Columns.Add(btn);    
           
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
            show();
           
        }
        private void uname()
        {
            string admi = "";
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\IT Inventory management\\IT Inventory Database.accdb");
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandText = "SELECT UserRole FROM Login WHERE Username='" + sa + "' ";
            con.Open();
            OleDbDataReader adm;
            adm = cmd.ExecuteReader();
        }

        private void Admin_Control_Load(object sender, EventArgs e)
        {

            sa = aa.name; **
            lbl_name.Text = aa.name;

            uname(); 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            New_User nu = new New_User();
            nu.Show();
            this.Close();
        }

       
            
        }

        
}
This is the code and I get Error on the code marked with **

What I have tried:

This is the code and I get Error on the code marked with **
Posted
Updated 10-May-18 0:07am
Comments
F-ES Sitecore 10-May-18 5:56am    
"aa" will be null, and you only set it in Admin_Control to be "s1" so either Admin_Control isn't being called or "s1" is also null. From what you've posted we can't say much more than that. Learn to debug through your code.
Member 13820524 10-May-18 6:17am    
Actually the value of aa is coming from the code below:

namespace IT_Inventory_Management
{
public partial class Main : Form
{

Form1.User aa;
public string sa;
private Form1.User ua;

public Main(Form1.User s1)
{
InitializeComponent();
aa = s1;
this.ua = ua;

}

private void Main_Load(object sender, EventArgs e)
{
sa = aa.name;
lbl_name.Text = aa.name;
admin();
}

private void admin()
{
string admi = "";
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\IT Inventory management\\IT Inventory Database.accdb");
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT UserRole FROM Login WHERE Username='"+sa+"' ";
con.Open();
OleDbDataReader adm;
adm= cmd.ExecuteReader();
adm.Read();
admi = adm["UserRole"].ToString();
if (admi.Equals("Admin"))
{
btn_admin.Visible = true;
}
}

private void label1_Click(object sender, EventArgs e)
{

}
Member 13820524 10-May-18 6:18am    
and entirely linked via below code:

namespace IT_Inventory_Management
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class User
{
public string name;

}
private void login()
{
//Provider=Microsoft.ACE.OLEDB.12.0;Data Source="|DataDirectory|\Inventory Database.accdb"
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\IT Inventory management\\IT Inventory Database.accdb");

OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT Username, Password FROM Login WHERE Username='" + txt_user.Text + "' AND Password='" + txt_pword.Text + "' ";
con.Open();
OleDbDataReader myReader;
myReader = cmd.ExecuteReader();
int count = 0;

while (myReader.Read())
{
count = count + 1;

}
if (count == 1)
{

User ua = new User();
ua.name = txt_user.Text;
Main lbl = new Main(ua);
lbl.Show();
this.Hide();
txt_user.Text = "";
txt_pword.Text = "";
}
else if (txt_user.Text.Length == 0 || txt_pword.Text.Length == 0)
{
MessageBox.Show("Please Enter The Credentials ");
}

else
{

MessageBox.Show("Login Failed");
txt_user.Text = "";
txt_pword.Text = "";
}
}

private void button1_Click(object sender, EventArgs e)
{
this.AcceptButton = button1;
login();
}
F-ES Sitecore 10-May-18 6:20am    
That's a completely different form. You might have variables called "aa" on both forms but they two different variables that just share a name. It's liking having two drawers both marked "Forks". If you put a fork in one drawer that doesn't mean that fork is in the other drawer as well.
Member 13820524 10-May-18 6:16am    
Actually the value of aa is coming from another form below is the code:

namespace IT_Inventory_Management
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class User
{
public string name;

}
private void login()
{
//Provider=Microsoft.ACE.OLEDB.12.0;Data Source="|DataDirectory|\Inventory Database.accdb"
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\IT Inventory management\\IT Inventory Database.accdb");

OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT Username, Password FROM Login WHERE Username='" + txt_user.Text + "' AND Password='" + txt_pword.Text + "' ";
con.Open();
OleDbDataReader myReader;
myReader = cmd.ExecuteReader();
int count = 0;

while (myReader.Read())
{
count = count + 1;

}
if (count == 1)
{

User ua = new User();
ua.name = txt_user.Text;
Main lbl = new Main(ua);
lbl.Show();
this.Hide();
txt_user.Text = "";
txt_pword.Text = "";
}
else if (txt_user.Text.Length == 0 || txt_pword.Text.Length == 0)
{
MessageBox.Show("Please Enter The Credentials ");
}

else
{

MessageBox.Show("Login Failed");
txt_user.Text = "";
txt_pword.Text = "";
}
}

private void button1_Click(object sender, EventArgs e)
{
this.AcceptButton = button1;
login();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//New_User nu = new New_User();
//nu.Show();
//this.Hide();
}

private void groupBox1_Enter(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{


}
}
}

See the comment from F-ES Sitecore. aa has not been initialised.

Only you can track this through - use this article to guide your through the debugging process: Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
Comments
Wendelius 10-May-18 7:01am    
Exactly!
This error is about you initialized Admin_Control with parameterless constructor instead of "new Admin_Control(Form1.User s1)". where s1 must be not null.
 
Share this answer
 
Comments
CHill60 10-May-18 7:40am    
? We can't see how the OP initialized Admin_Control ?

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