0

I browsed a lot on StackOverflow and all over the internet but couldn't find anything that could help me. I am putting some information from a SQL database onto a DataGridView, and i am adding an extra column with checkboxes.

 private void populatedataGrid()
        {
            String sql = "SELECT pm.Name, pm.telephone, pm.email, pm.validID, comp.name as `Company` FROM `project managers`as pm JOIN `companies`as comp ON pm.Companies_companyID = comp.companyID where pm.Companies_companyID =" + loginID;
            MySqlCommand command = new MySqlCommand(sql, dh.Connection);

            try
            {
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = command;
                DataTable dbdataset = new DataTable();
                adapter.Fill(dbdataset);
                BindingSource bSource = new BindingSource();

                // DataGridView1 is a different DataGrid, i am working on DataGridView2
                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                dataGridView2.DataSource = bSource;
                //Method for adding the additional column with checkboxes (ill paste the method below)
                addCheckBoxColumn();
                //I make it so that only the checkboxes can be edited
                foreach (DataGridViewColumn dc in dataGridView2.Columns)
                {
                    if (dc.Index.Equals(5))
                    {
                        dc.ReadOnly = false;
                    }
                    else
                    {
                        dc.ReadOnly = true;
                    }
                }
                adapter.Update(dbdataset);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                dh.Connection.Close();
            }

}

Here is the method for creating the column with Checkboxes:

 private void addCheckBoxColumn()
        {
            DataGridViewCheckBoxColumn cbCol = new DataGridViewCheckBoxColumn();
            cbCol.ValueType = typeof(bool);
            cbCol.Name = "Select";
            cbCol.HeaderText = "Select";
            dataGridView2.Columns.Add(cbCol);
        }

And it all works fine for now: DataGridView

I created a button for testing purposes, which i want when clicked to write on label2 the Name for the checkbox that is checked. So here`s the method i wrote:

private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                DataGridViewCheckBoxCell cbxcell = row.Cells[5] as DataGridViewCheckBoxCell;

                if ((bool)cbxcell.Value == true)
                {
                    label2.Text = row.Cells[1].Value.ToString();
                }
            }
        }

When i run it and click Button1 i get the following Exception: "An unhandled exception of type 'System.NullReferenceException' occurred in BuildID-Company-Backend.exe

Additional information: Object reference not set to an instance of an object."

Any ideas? :(

  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Feb 20 '15 at 15:35
  • Note that the CheckBox __looks__ the same when a) the value is false or b) the value is not set, ie null. – TaW Feb 20 '15 at 17:13

0 Answers0