-3

I'm beginner in C#. I have some buttons which are disabled by default in the main form. After the user signs in I want them to be enabled and available for the user to work with. It's kind of an access modifier.

The buttons are in the main form and the login form is within another form. After user pressed a login button (checking the username and password) i want to enable those button in the main form.below is my code

      private void btnLogic_Click(object sender, EventArgs e)
    {
        FormSettingLogic f2 = new FormSettingLogic();
        f2.Owner = this;
        f2.Show();
    }


    private void btnAddFile_Click(object sender, EventArgs e)
    {
        FormLogin fl = new FormLogin();
        fl.Owner = this;
        ....
        ....
     }

private void btnLoadManteg_Click(object sender, EventArgs e)
    {
        Form_LoadMantegh f6 = new Form_LoadMantegh();
        f6.Owner = this;
        f6.Show();
    }

and user's login code

public FormLogin()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {



        String abc;

        try
        {
            sqlConnection1.Open();
            SqlCommand cmd = new SqlCommand("select * from [Users] where userName=@uName and password = @Pass ", sqlConnection1);

            SqlDataAdapter sda;
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            sda.SelectCommand.Parameters.AddWithValue("@uName", txtUname.Text);
            sda.SelectCommand.Parameters.AddWithValue("@Pass", txtpass.Text);
            abc =Convert.ToString (cmd.ExecuteScalar()); //execute the query and get the user Id and put it in abc variable
            DataTable dt = new DataTable();
            sda.Fill(dt);
            sqlConnection1.Close();
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Username or Password is incorrect!");
            }
            else
            {

                this.Hide();
                //MessageBox.Show(abc);
                sendID.send = abc;

                (this.Owner as Form_Main).btnAddFile.Enabled = true;
                (this.Owner as Form_Main).btnLogic.Enabled = true;

            }



        }

        catch(Exception err)
        {
            MessageBox.Show(err.Message);
        }

Form_Main_load code :

private void Form_Main_Load(object sender, EventArgs e)
    {

        btnLogic.Enabled = false;
        btnAddFile.Enabled = false;
        btnLoadManteg.Enabled = false; 

    }

and the Error is: "Object reference not set to an instance of an object"

Mo0rteza
  • 320
  • 6
  • 18
  • have you tried anything yet? 1 way would be to pass the information back to the main form via a `bool` flag or the `DialogResult` and act according to this flag. Or you could pass the instance of the main form to the login form and handle the buttons directly (which is a little messy). – Mong Zhu May 08 '17 at 06:53
  • make a `public static Boolean variable` in your main form and after success login via second form, change he Boolean value to `true` which means the buttons should enable. – Inside Man May 08 '17 at 06:54
  • Possible duplicate of [How to access a form control for another form?](http://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) – M. Adeel Khalid May 08 '17 at 06:56
  • Declare a flag Variable , if logged in successfully than set flag=true and use enable property of buttons , if flag is false (in case of login failed) do nothing – Saurabh May 08 '17 at 09:30

1 Answers1

0

if user pass the login set userName but user can not pass the login set userName to null

 public Form1()
    {
        InitializeComponent();
        button1.Visible = false;
    }
    public void AfterLoginMethod()
    {
        if (userName!=null)//You can use another control like userId > 0 ...
            button1.Visible = true;
    }
Mehmet Topçu
  • 1
  • 1
  • 16
  • 31
  • what if the username was wrong at the first attempt and the user decides to cancel the login process. Then the button would still be released to be visible and the login becomes useless... – Mong Zhu May 08 '17 at 07:49
  • Your are right , but I thought that he could do this "if user pass the login set userName but user can not pass the login set userName to null" – Mehmet Topçu May 08 '17 at 08:13