Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

Code works Fine. But While Filtering dataGridView2 i got an error"Object reference not set to an instance of an object"
C#
private void Form1_Load(object sender, EventArgs e)
       {
           BindGridForExcel();
           BindGridForSql();
       }

       private void BindGridForExcel()
       {
           try
           {
               OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;data source=D:\\DotNet\\Copy of Depreciation working Oct-13.xlsx ;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
               theConnection.Open();
               OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Working$]", theConnection);
               DataSet theDS = new DataSet();
               DataTable dt = new DataTable();
               theDataAdapter.Fill(dt);
               this.dataGridView1.DataSource = dt.DefaultView;
               theConnection.Close();
           }
           catch (Exception ex)
           {

           }

       }

       private void BindGridForSql()
       {
           string connectionString = "Data Source=192.168.0.11;Initial Catalog=30_Nov_13;User ID=sa;Password=super";
           string sql = "SELECT * from [@BA_ODPV] ";

           using (var connection = new SqlConnection(connectionString))
           using (var command = new SqlCommand(sql, connection))
           using (var adapter = new SqlDataAdapter(command))
           {
               connection.Open();
               var dataEntry1 = new DataTable();
               adapter.Fill(dataEntry1);
               dataGridView2.DataSource = dataEntry1;
           }
       }

       private void dataGridView1_DoubleClick(object sender, EventArgs e)
       {
           DataGridViewSelectedRowCollection rc = null;
           rc = dataGridView1.SelectedRows;
           itemcode = rc[0].Cells["F1"].Value.ToString();
           for (int i = 0; i < dataGridView2.Rows.Count; i++)
           {
               if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode)
               {
                   dataGridView2.Rows[i].Selected = true;
                   dataGridView2.Rows[i].Visible = true;
               }
           }
       }
Posted
Comments
Debug and check on which line it throws this exception?
xibit89 23-Nov-13 1:15am    
exactly on if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode) line
That means there is no value at cell 2 position.
xibit89 23-Nov-13 2:51am    
there is value in cell 2 and it also comparing it with itemcode, after completing the loop it generate the exception.

1 solution

Possible errors :


whenever u try to access an object index,, please make sure that the object is not null...


try adding this line
C#
 if( dt != null )
 above
this.dataGridView1.DataSource = dt.DefaultView;



try adding this line
if( rc != null && rc.count > )
 if( rc[0].cells.count > 0)
 itemcode = rc[0].Cells["F1"].Value.ToString();


if( dataGridView2 != null && datagridview2.rows.count > 0)
for (int i = 0; i < dataGridView2.Rows.Count; i++)




if( datagridview2.rows[i].cells.count >2 )
if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode)
 
Share this answer
 
Comments
xibit89 23-Nov-13 1:13am    
i dont understand the logic of above code u provided, can u be plz explain this in simple way.. and can u plz tell me that object that is said to be null..??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900