none
Issue with data sharing between forms RRS feed

  • Question

  • Hello,

    Question from beginner

    I have a problem when sharing data between 2 forms. I used this before but not working for me anymore

      I'm using public class to pass the data on other form.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace formsdatashare
    {
        public class Dane1
        {
            public string id { get; set; }
        }
    }
    

    form 1:

    namespace formsdatashare
    {
        public partial class Form1 : Form
        {
            public Dane1 dnss { get; set; }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                dnss.id = "ttst";
            }
        }
    }
    

    Problem is when I press button I'm getting Error: 

    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    formsdatashare.Form1.dnss.get returned null.

    Then I change the code on Form1 to:

    namespace formsdatashare
    {
        public partial class Form1 : Form
        {
            //  public Dane1 dnss { get; set; }
            Dane1 dnss =new Dane1();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                dnss.id = "Check";
                odbior otherform = new odbior();
                otherform.Show();
            }
        }
    }

    but no data  passes on the other form 

    Form 2

    namespace formsdatashare
    {
        public partial class odbior : Form
        {
            // public Dane1 tr { get; set; }
            Dane1 tr = new Dane1();
            public odbior()
            {
                InitializeComponent();
            }
    
            private void odbior_Load(object sender, EventArgs e)
            {
                label1.Text = tr.id;
            }
        }
    }

     

    Tuesday, April 3, 2018 2:56 PM

Answers

  • May This Help You

    Following Code Should Work For You

    public class Dane1
        {
                 
            public string id { get; set; }
           
        }

    public partial class odbior : Form
        {
            // public Dane1 tr { get; set; }
            public Dane1 tr = new Dane1();
            public odbior()
            {
                InitializeComponent();
            }
    
    
            private void odbior_Load(object sender, EventArgs e)
            {
                label1.Text = tr.id;
            }
        }

     public partial class Form1 : Form
        {
            //  public Dane1 dnss { get; set; }
            Dane1 dnss = new Dane1();
            odbior odb1 = new odbior();
            public Form1()
            {
                InitializeComponent();
                odb1.tr = dnss;
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                dnss.id = "Check";
                odb1.Show();
            }
        }
    Best Regards
    Vikram Manjare
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    • Marked as answer by gwizdek Tuesday, April 3, 2018 3:47 PM
    Tuesday, April 3, 2018 3:20 PM
  • Thanks Vikram your solution worked well
    • Marked as answer by gwizdek Tuesday, April 3, 2018 3:48 PM
    Tuesday, April 3, 2018 3:48 PM

All replies

  • May This Help You

    Following Code Should Work For You

    public class Dane1
        {
                 
            public string id { get; set; }
           
        }

    public partial class odbior : Form
        {
            // public Dane1 tr { get; set; }
            public Dane1 tr = new Dane1();
            public odbior()
            {
                InitializeComponent();
            }
    
    
            private void odbior_Load(object sender, EventArgs e)
            {
                label1.Text = tr.id;
            }
        }

     public partial class Form1 : Form
        {
            //  public Dane1 dnss { get; set; }
            Dane1 dnss = new Dane1();
            odbior odb1 = new odbior();
            public Form1()
            {
                InitializeComponent();
                odb1.tr = dnss;
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                dnss.id = "Check";
                odb1.Show();
            }
        }
    Best Regards
    Vikram Manjare
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    • Marked as answer by gwizdek Tuesday, April 3, 2018 3:47 PM
    Tuesday, April 3, 2018 3:20 PM
  • Thanks Vikram your solution worked well
    • Marked as answer by gwizdek Tuesday, April 3, 2018 3:48 PM
    Tuesday, April 3, 2018 3:48 PM