0

At this line of code dr = dt.Rows[k]; I am getting the exception Object reference not set to an instance of an object. where I am going wrong?

public partial class EditEngClgList : Form
        { 
          public EditEngClgList()
            {
                InitializeComponent();
                try
                {
                    acccon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
                    acccon.Open();
                }
                catch (Exception err)
                {
                    MessageBox.Show("Error:" + err);
                }
                string sql = "Select * From EngColeges order by EngClgID";
                da = new OleDbDataAdapter(sql, acccon);
                cmdb = new OleDbCommandBuilder(da);

                dt1 = new DataTable();
                da.Fill(dt1);
                bs = new BindingSource();
                bs.DataSource = dt1;

                dataGridView1.DataSource = bs;
                dataGridView1.Columns[1].Visible = false;
            }
     private void button4_Click(object sender, EventArgs e)
            {
                List<int> checkedclg = new List<int>();
                DataRow dr;
                List<int> checkedclgid = new List<int>();
                for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
                {
                    if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Delete"].Value) == true)
                    {
                        checkedclg.Add(i);
                        checkedclgid.Add(Convert.ToInt16(dataGridView1.Rows[i].Cells["Delete"].Value)); 
                    }
                }
                foreach (int k in checkedclg)
                {
                    dr = dt.Rows[k]; //this datatable object I hae created in another function              
                    dt.Rows[k].Delete();
                    foreach (int j in checkedclgid)
                    {
                        OleDbCommand oleDbCommand = new OleDbCommand("DELETE FROM EngColeges WHERE EngClgID = @clgID", acccon);
                        oleDbCommand.Parameters.Add("@clgID", OleDbType.Integer).Value = j;
                        oleDbCommand.Prepare();
                        oleDbCommand.ExecuteNonQuery();

                    }
                }
            }

Thanks for any help

1
  • k is probably larger than dt.Rows.Count Commented Aug 29, 2013 at 4:32

1 Answer 1

2

Place a breakpoint on that line - most likely the dt object is null.

To address the comment that k may be larger than Rows.Count, I would think if the problem was with k, you'd get an exception indicating the index is out of bounds (IndexOutOfBoundsException).

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting NullreferenceException was Unhandeled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.