Abdu Abdul

Abdu Abdul

  • 1.3k
  • 409
  • 15.1k

Additional information: object reference not set to an instance object

Dec 4 2021 12:39 PM

Am retrieving data from database to datagridview C# windows Form on form load, before it was working fine on normal retrieval, upon adding the try and catch block and await Task.Run() it display the message below... I don't know what is wrong again.

An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dllAdditional information: Object reference not set to an instance of an object.

protected void Bind_Data()
{
    try
    {
        Control.CheckForIllegalCrossThreadCalls = false;
        connect.connect.Open();
        Thread.Sleep(500);
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM table_A ORDER BY id ASC", connect.connect);
       
        MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        this.Datagridview.DataSource = dt;

        this.Datagridview.Columns["date_created"].Visible = false;
        this.Datagridview.Columns["Time"].Visible = false;
        connect.connect.Close();

        count_DGV_lbl.Text = $"Total Records: {Datagridview.RowCount}";
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Warning Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

 


Answers (4)