title: How to display data from database to another form by calling it from another form
body: <p>I have created a form in C# for the user to login and when they successfully login, the data of the user who had logged in will be displayed to another form in a gridview. The problem here is when the user successfully logged in then goes to the other form the data was not displayed and i get an error "object reference not set to an instance of an object".</p>
  
  <p>here is my code:
  LoginFaculty.cs</p>
  
  <pre><code> using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;
   using MySql.Data.MySqlClient;
  namespace ClassScheduling
  {
  public partial class LogInFaculty : Form
  {
      public LogInFaculty()
      {
          InitializeComponent();
      }
  
      private void btnSignInFaculty_Click(object sender, EventArgs e)
      {
          try
          {
              string myConnection = "datasource=localhost;port=3306;username=root;password=root";
              MySqlConnection myConn = new MySqlConnection(myConnection);
              MySqlCommand SelectCommand = new MySqlCommand(" select * from database.faculty where FullName='" + this.txtBoxUserNameFaculty.Text + "' and Password= '" + this.txtBoxPasswordFaculty.Text + "'", myConn);
              MySqlDataReader myReader;
              myConn.Open();
              myReader = SelectCommand.ExecuteReader();
              string lastname = myReader.ToString();
  
              int count = 0;
              while (myReader.Read())
              {
                  count = count + 1;
              }
              if (count == 1)
              {
  
                  MessageBox.Show("LogIn Sucessfully");
                  this.Hide();
  
                  FacultyLoadForm newForm = new FacultyLoadForm();
                  newForm.Show();
              }
              else if (count &gt; 1)
              {
                  MessageBox.Show("Duplicate Username and password ....Access Denied!");
              }
              else
                  MessageBox.Show("Username or Password is incorrect");
  
              myConn.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      private void btnExitFaculty_Click(object sender, EventArgs e)
      {
          Account newForm = new Account();
          newForm.Show();
          this.Hide();
      }
  }
  }
  </code></pre>
  
  <p>then for my FacultyLoad.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using MySql.Data.MySqlClient;
  
  namespace ClassScheduling
  {
  public partial class LogInFaculty : Form
  {
      public LogInFaculty()
      {
          InitializeComponent();
      }
  
      private void btnSignInFaculty_Click(object sender, EventArgs e)
      {
          try
          {
              string myConnection = "datasource=localhost;port=3306;username=root;password=root";
              MySqlConnection myConn = new MySqlConnection(myConnection);
              MySqlCommand SelectCommand = new MySqlCommand(" select * from database.faculty where FullName='" + this.txtBoxUserNameFaculty.Text + "' and Password= '" + this.txtBoxPasswordFaculty.Text + "'", myConn);
              MySqlDataReader myReader;
              myConn.Open();
              myReader = SelectCommand.ExecuteReader();
              string lastname = myReader.ToString();
  
              int count = 0;
              while (myReader.Read())
              {
                  count = count + 1;
              }
              if (count == 1)
              {
  
                  MessageBox.Show("LogIn Sucessfully");
                  this.Hide();
  
                  FacultyLoadForm newForm = new FacultyLoadForm();
                  newForm.Show();
              }
              else if (count &gt; 1)
              {
                  MessageBox.Show("Duplicate Username and password ....Access Denied!");
              }
              else
                  MessageBox.Show("Username or Password is incorrect");
  
              myConn.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      private void btnExitFaculty_Click(object sender, EventArgs e)
      {
          Account newForm = new Account();
          newForm.Show();
          this.Hide();
      }
  }
  }
  </code></pre>
  
  <p>That's it! I really have no idea why I keep getting that error and I've tried every possible solution I know. If anyone know, please do tell me.</p>