-1

I am trying to pass a datable from one form to another. Datable has been retrieve successfully from database but program is throwing a Null reference exception error when deploying on the device. My code for

form 1:

if (comboBox1.SelectedItem.ToString() == "Select by Picture")
            {
                MySqlConnection con = new MySqlConnection("server=******;port= *****;database=********;User Id=root;Password=;");
                MySqlDataAdapter d_a = new MySqlDataAdapter("Select Picture From sampledata", con);
                DataTable d_t = new DataTable();
                d_a.Fill(d_t);
                int count = d_t.Rows.Count;
                PictureSelection ps = new PictureSelection();
                if (count > 0)
                ps.Storelist.Items.Clear();
                for (int i=0 ; i <count; i++)
                {
                    ps.Storelist.Items.Add(d_t.Rows[i][0].ToString());
                }
                ps.ShowDialog();
            }
            else if (comboBox1.SelectedItem.ToString() == "Select by Artist Name")
            {
                MySqlConnection con = new MySqlConnection("server=*****;port= ****;database=*****;User Id=root;Password=;");
                MySqlDataAdapter d_a = new MySqlDataAdapter("Select Artist Name From sampledata", con);
                DataTable d_t = new DataTable();
                d_a.Fill(d_t);
                int count = d_t.Rows.Count;
                PictureSelection ps = new PictureSelection();
                if (count > 0)
                ps.Storelist.Items.Clear();
                for (int i = 0; i < count; i++)
                {
                    ps.Storelist.Items.Add(d_t.Rows[i][0].ToString());
                }
                ps.ShowDialog();
            }
        }

Form2:

private void PictureSelection_Load(object sender, EventArgs e)
        {

            Storelist.DataSource = mainmn.d_t;


        }

Any help will be appreciated.

prince
  • 17
  • 1
  • 1
  • 7
  • 1
    http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – David Nov 16 '15 at 21:00
  • Declaring a local variable with the same name of a global one doesn't make the two the same and a local variable is not accessible from another class. – Steve Nov 16 '15 at 21:07

1 Answers1

0

You have to set datatable from form1:

ps.Storelist.Datasource = d_t;
ps.ShowDialog();

because form2 have no definition of datatable and an error occurs.

c4pricorn
  • 3,471
  • 1
  • 11
  • 12