1

I want the username entered in login form available in other forms.

Form1:

    private void BtnLogin_Click(object sender, EventArgs e)
    {
         .....

         FrmCreate_Invoice ci = new FrmCreate_Invoice(txtUsername.Text);

         .....
    }

Form2:

    public FrmCreate_Invoice(string usrnam)
    {
        InitializeComponent();
        string ausr = usrnam.ToString();
        label1.Text = ausr;
    }

}

Ameena
  • 187
  • 2
  • 5
  • 18
  • 1
    On which line are you getting `NullReferenceException` – Nikolay Kostov Feb 08 '15 at 14:07
  • I guess `label1` is null? because if so you need to uncomment the `InitializeComponent()` line – Tomer Feb 08 '15 at 14:09
  • on this line ' label1.Text = ausr;' @Nikolay – Ameena Feb 08 '15 at 14:10
  • @tomer. yes u r right. the error is now solved. but i get label.text's value as 'label1' now. – Ameena Feb 08 '15 at 14:11
  • i want the txtusrname entered in login form to b the label.text's value – Ameena Feb 08 '15 at 14:12
  • How is that possible that `label1.Text` returns the name? What does `ausr` contain? – Tomer Feb 08 '15 at 14:17
  • so you're simply doing `label1.Text = ausr` and right after this line `label1.Text` contains something else? or does it return something else after a few other lines of code? – Tomer Feb 08 '15 at 14:22
  • @tomer , i resolved the issue. i had used FrmCreate_Invoice ci = new FrmCreate_Invoice(); again in the code below , so it had initiallized all the values. thats y i din get label.text. thank you. i now used property to get value from the parent form – Ameena Feb 08 '15 at 14:59

1 Answers1

0

You are getting NullReferenceException because label1 is null.

As @Tomer suggested, uncomment InitializeComponent();.

Without InitializeComponent no components will be initialized therefore their values will be null.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • Thank you. After i uncommented the initialize component line the error is not coming. but the label.text value still remains 'label1' :( – Ameena Feb 08 '15 at 14:15